2011-12-19 11:07:59 +01:00
|
|
|
/**
|
2013-01-09 01:47:48 +01:00
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
2011-12-19 11:07:59 +01:00
|
|
|
*/
|
2012-05-22 11:37:09 +02:00
|
|
|
package docs.actor
|
2011-12-13 11:04:23 +01:00
|
|
|
|
2012-06-28 15:33:49 +02:00
|
|
|
import language.postfixOps
|
|
|
|
|
|
2011-12-13 11:04:23 +01:00
|
|
|
//#imports1
|
|
|
|
|
import akka.actor.Actor
|
|
|
|
|
import akka.actor.Props
|
2012-09-21 14:50:06 +02:00
|
|
|
import scala.concurrent.duration._
|
2011-12-13 11:04:23 +01:00
|
|
|
|
|
|
|
|
//#imports1
|
|
|
|
|
|
|
|
|
|
import org.scalatest.{ BeforeAndAfterAll, WordSpec }
|
|
|
|
|
import org.scalatest.matchers.MustMatchers
|
|
|
|
|
import akka.testkit._
|
|
|
|
|
|
|
|
|
|
class SchedulerDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
|
|
|
|
|
"schedule a one-off task" in {
|
|
|
|
|
//#schedule-one-off-message
|
2012-08-08 15:57:30 +02:00
|
|
|
//Use the system's dispatcher as ExecutionContext
|
|
|
|
|
import system.dispatcher
|
|
|
|
|
|
2011-12-13 11:04:23 +01:00
|
|
|
//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
|
2013-01-14 23:21:51 +01:00
|
|
|
//Schedules a function to be executed (send a message to the testActor) after 50ms
|
2011-12-13 11:04:23 +01:00
|
|
|
system.scheduler.scheduleOnce(50 milliseconds) {
|
2011-12-14 21:52:39 +01:00
|
|
|
testActor ! System.currentTimeMillis
|
2011-12-13 11:04:23 +01:00
|
|
|
}
|
|
|
|
|
//#schedule-one-off-thunk
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"schedule a recurring task" in {
|
2013-04-14 22:56:41 +02:00
|
|
|
new AnyRef {
|
|
|
|
|
//#schedule-recurring
|
|
|
|
|
val Tick = "tick"
|
|
|
|
|
class TickActor extends Actor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case Tick ⇒ //Do something
|
|
|
|
|
}
|
2011-12-13 11:04:23 +01:00
|
|
|
}
|
2013-04-14 22:56:41 +02:00
|
|
|
val tickActor = system.actorOf(Props(classOf[TickActor], this))
|
|
|
|
|
//Use system's dispatcher as ExecutionContext
|
|
|
|
|
import system.dispatcher
|
2012-08-08 15:57:30 +02:00
|
|
|
|
2013-04-14 22:56:41 +02:00
|
|
|
//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)
|
2011-12-13 11:04:23 +01:00
|
|
|
|
2013-04-14 22:56:41 +02:00
|
|
|
//This cancels further Ticks to be sent
|
|
|
|
|
cancellable.cancel()
|
|
|
|
|
//#schedule-recurring
|
|
|
|
|
system.stop(tickActor)
|
|
|
|
|
}
|
2011-12-13 11:04:23 +01:00
|
|
|
}
|
|
|
|
|
}
|