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-14 21:52:39 +01:00
|
|
|
|
|
|
|
|
//#imports1
|
|
|
|
|
import akka.actor.Props;
|
2012-10-15 16:18:52 +02:00
|
|
|
import scala.concurrent.duration.Duration;
|
2011-12-14 21:52:39 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
//#imports1
|
|
|
|
|
|
|
|
|
|
//#imports2
|
|
|
|
|
import akka.actor.UntypedActor;
|
|
|
|
|
import akka.actor.Cancellable;
|
|
|
|
|
//#imports2
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.testkit.AkkaSpec;
|
2013-05-02 17:12:36 +02:00
|
|
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
|
|
|
|
import org.junit.*;
|
2011-12-14 21:52:39 +01:00
|
|
|
|
2013-05-08 09:42:25 +02:00
|
|
|
public class SchedulerDocTest {
|
2011-12-14 21:52:39 +01:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
@Rule
|
|
|
|
|
public AkkaJUnitActorSystemResource actorSystemResource =
|
|
|
|
|
new AkkaJUnitActorSystemResource("SchedulerDocTest", AkkaSpec.testConf());
|
2011-12-14 21:52:39 +01:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
|
|
|
|
private ActorRef testActor = system.actorOf(Props.create(MyUntypedActor.class));
|
2011-12-14 21:52:39 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void scheduleOneOffTask() {
|
|
|
|
|
//#schedule-one-off-message
|
2012-09-26 10:56:25 +02:00
|
|
|
system.scheduler().scheduleOnce(Duration.create(50, TimeUnit.MILLISECONDS),
|
2013-01-14 23:21:51 +01:00
|
|
|
testActor, "foo", system.dispatcher(), null);
|
2011-12-14 21:52:39 +01:00
|
|
|
//#schedule-one-off-message
|
|
|
|
|
|
|
|
|
|
//#schedule-one-off-thunk
|
2012-09-26 10:56:25 +02:00
|
|
|
system.scheduler().scheduleOnce(Duration.create(50, TimeUnit.MILLISECONDS),
|
|
|
|
|
new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
testActor.tell(System.currentTimeMillis(), null);
|
|
|
|
|
}
|
2012-08-08 15:57:30 +02:00
|
|
|
}, system.dispatcher());
|
2011-12-14 21:52:39 +01:00
|
|
|
//#schedule-one-off-thunk
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void scheduleRecurringTask() {
|
|
|
|
|
//#schedule-recurring
|
2013-04-14 22:56:41 +02:00
|
|
|
class Ticker extends UntypedActor {
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Object message) {
|
|
|
|
|
if (message.equals("Tick")) {
|
|
|
|
|
// Do someting
|
|
|
|
|
} else {
|
|
|
|
|
unhandled(message);
|
2012-09-26 10:56:25 +02:00
|
|
|
}
|
2013-04-14 22:56:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ActorRef tickActor = system.actorOf(Props.create(Ticker.class, this));
|
2011-12-14 21:52:39 +01:00
|
|
|
|
|
|
|
|
//This will schedule to send the Tick-message
|
|
|
|
|
//to the tickActor after 0ms repeating every 50ms
|
2012-09-26 10:56:25 +02:00
|
|
|
Cancellable cancellable = system.scheduler().schedule(Duration.Zero(),
|
|
|
|
|
Duration.create(50, TimeUnit.MILLISECONDS), tickActor, "Tick",
|
2012-12-18 00:51:11 +01:00
|
|
|
system.dispatcher(), null);
|
2011-12-14 21:52:39 +01:00
|
|
|
|
|
|
|
|
//This cancels further Ticks to be sent
|
|
|
|
|
cancellable.cancel();
|
|
|
|
|
//#schedule-recurring
|
|
|
|
|
system.stop(tickActor);
|
|
|
|
|
}
|
|
|
|
|
}
|