From ec1c108838ca9ec8dca1f5f93a7c9d3ccb8419ae Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Tue, 13 Dec 2011 11:04:23 +0100 Subject: [PATCH] More docs --- akka-docs/common/code/SchedulerDocSpec.scala | 69 ++++++++++++++++++++ akka-docs/common/scheduler.rst | 19 ++++++ 2 files changed, 88 insertions(+) create mode 100644 akka-docs/common/code/SchedulerDocSpec.scala diff --git a/akka-docs/common/code/SchedulerDocSpec.scala b/akka-docs/common/code/SchedulerDocSpec.scala new file mode 100644 index 0000000000..18cb80faf5 --- /dev/null +++ b/akka-docs/common/code/SchedulerDocSpec.scala @@ -0,0 +1,69 @@ +package akka.scheduler.actor + +//#imports1 +import akka.actor.Actor +import akka.actor.Props +import akka.util.duration._ + + +//#imports1 + +import org.scalatest.{ BeforeAndAfterAll, WordSpec } +import org.scalatest.matchers.MustMatchers +import akka.testkit._ +import akka.util.duration._ + +class SchedulerDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) { + "schedule a one-off task" in { + //#schedule-one-off-message + //Schedules to send the "foo"-message to the testActor after 50ms + system.scheduler.scheduleOnce(50 milliseconds, testActor, "foo") + //#schedule-one-off-message + + expectMsg(1 second, "foo") + + //#schedule-one-off-thunk + //Schedules to send the "foo"-message to the testActor after 50ms + system.scheduler.scheduleOnce(50 milliseconds) { + testActor ! "foo" + } + //#schedule-one-off-thunk + + expectMsg(1 second, "foo") + + //#schedule-one-off-runnable + //Schedules to send the "foo"-message to the testActor after 50ms + system.scheduler.scheduleOnce( + 50 milliseconds, + new Runnable { + def run = testActor ! "foo" + } + ) + + //#schedule-one-off-runnable + + expectMsg(1 second, "foo") + } + + "schedule a recurring task" in { + //#schedule-recurring + val Tick = "tick" + val tickActor = system.actorOf(Props(new Actor { + def receive = { + case Tick ⇒ //Do something + } + })) + //This will schedule to send the Tick-message + //to the tickActor after 0ms repeating every 50ms + val cancellable = + system.scheduler.schedule(0 milliseconds, + 50 milliseconds, + tickActor, + Tick) + + //This cancels further Ticks to be sent + cancellable.cancel() + //#schedule-recurring + tickActor.stop() + } +} diff --git a/akka-docs/common/scheduler.rst b/akka-docs/common/scheduler.rst index d7a3aeedb6..d05cea60aa 100644 --- a/akka-docs/common/scheduler.rst +++ b/akka-docs/common/scheduler.rst @@ -7,6 +7,25 @@ of akka.actor.Scheduler, this instance is unique per ActorSystem and is used int to happen at specific points in time. Please note that the scheduled tasks are executed by the default ``MessageDispatcher`` of the ``ActorSystem``. +You can schedule sending of messages to actors and execution of tasks (functions or Runnable). +You will get a ``Cancellable`` back that you can call :meth:``cancel`` on to cancel the execution of the +scheduled operation. + +Some examples +------------- + +.. includecode:: code/SchedulerDocSpec.scala + :include: imports1,schedule-one-off-message + +.. includecode:: code/SchedulerDocSpec.scala + :include: imports1,schedule-one-off-thunk + +.. includecode:: code/SchedulerDocSpec.scala + :include: imports1,schedule-one-off-runnable + +.. includecode:: code/SchedulerDocSpec.scala + :include: imports1,schedule-recurring + From ``akka.actor.ActorSystem`` -------------------------------