Last active
October 15, 2016 02:21
-
-
Save wsargent/b3d59fdc09f4fe02088c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package backend | |
import akka.actor.{Actor, ActorRef, Cancellable} | |
import scala.concurrent.duration.FiniteDuration | |
import scala.util.control.NoStackTrace | |
private case class SchedulerException(msg: String) extends akka.AkkaException(msg) with NoStackTrace | |
/** | |
* A scheduler which uses the "global default" dispatcher to schedule tasks. | |
* This class simplifies the use of scheduler so that people do not have to | |
* explicitly do "import context.dispatcher", with the additional benefit | |
* that the default dispatcher should make observed timer jitter smaller | |
* while context.dispatcher is experiencing load. | |
*/ | |
trait DefaultScheduler { | |
def context: akka.actor.ActorContext | |
/** | |
* Schedules a message to be sent repeatedly with an initial delay and | |
* frequency. E.g. if you would like a message to be sent immediately and | |
* thereafter every 500ms you would set delay=Duration.Zero and | |
* interval=Duration(500, TimeUnit.MILLISECONDS) | |
* | |
* Java & Scala API | |
*/ | |
final def schedule( | |
initialDelay: FiniteDuration, | |
interval: FiniteDuration, | |
receiver: ActorRef, | |
message: Any)(implicit sender: ActorRef = Actor.noSender): Cancellable = | |
schedule(initialDelay, interval, new Runnable { | |
def run = { | |
receiver ! message | |
if (receiver.isTerminated) | |
throw new SchedulerException("timer active for terminated actor") | |
} | |
}) | |
/** | |
* Schedules a function to be run repeatedly with an initial delay and a | |
* frequency. E.g. if you would like the function to be run after 2 seconds | |
* and thereafter every 100ms you would set delay = Duration(2, TimeUnit.SECONDS) | |
* and interval = Duration(100, TimeUnit.MILLISECONDS) | |
* | |
* Scala API | |
*/ | |
final def schedule( | |
initialDelay: FiniteDuration, | |
interval: FiniteDuration)(f: ⇒ Unit): Cancellable = | |
schedule(initialDelay, interval, new Runnable { override def run = f }) | |
/** | |
* Schedules a function to be run repeatedly with an initial delay and | |
* a frequency. E.g. if you would like the function to be run after 2 | |
* seconds and thereafter every 100ms you would set delay = Duration(2, | |
* TimeUnit.SECONDS) and interval = Duration(100, TimeUnit.MILLISECONDS) | |
* | |
* Java API | |
*/ | |
def schedule( | |
initialDelay: FiniteDuration, | |
interval: FiniteDuration, | |
runnable: Runnable): Cancellable = { | |
implicit val dispatcher = context.system.dispatcher | |
context.system.scheduler.schedule(initialDelay, interval, runnable) | |
} | |
/** | |
* Schedules a message to be sent once with a delay, i.e. a time period that has | |
* to pass before the message is sent. | |
* | |
* Java & Scala API | |
*/ | |
final def scheduleOnce(delay: FiniteDuration, | |
receiver: ActorRef, | |
message: Any)(implicit sender: ActorRef = Actor.noSender): Cancellable = { | |
scheduleOnce(delay, new Runnable { | |
override def run = receiver ! message | |
}) | |
} | |
/** | |
* Schedules a function to be run once with a delay, i.e. a time period that has | |
* to pass before the function is run. | |
* | |
* Scala API | |
*/ | |
final def scheduleOnce(delay: FiniteDuration)(f: ⇒ Unit): Cancellable = | |
scheduleOnce(delay, new Runnable { | |
override def run = f | |
}) | |
/** | |
* Schedules a Runnable to be run once with a delay, i.e. a time period that | |
* has to pass before the runnable is executed. | |
* | |
* Java & Scala API | |
*/ | |
def scheduleOnce(delay: FiniteDuration, | |
runnable: Runnable): Cancellable = { | |
implicit val dispatcher = context.system.dispatcher | |
context.system.scheduler.scheduleOnce(delay, runnable) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment