2011-12-19 11:07:59 +01:00
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
* Copyright (C) 2009-2012 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-06-29 13:33:20 +02:00
|
|
|
import scala.concurrent.util.Duration;
|
2011-12-14 21:52:39 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
//#imports1
|
|
|
|
|
|
|
|
|
|
//#imports2
|
|
|
|
|
import akka.actor.UntypedActor;
|
|
|
|
|
import akka.actor.UntypedActorFactory;
|
|
|
|
|
import akka.actor.Cancellable;
|
|
|
|
|
//#imports2
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.testkit.AkkaSpec;
|
|
|
|
|
|
|
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
public class SchedulerDocTestBase {
|
|
|
|
|
|
|
|
|
|
ActorSystem system;
|
|
|
|
|
ActorRef testActor;
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setUp() {
|
|
|
|
|
system = ActorSystem.create("MySystem", AkkaSpec.testConf());
|
2011-12-27 13:35:50 +01:00
|
|
|
testActor = system.actorOf(new Props(MyUntypedActor.class));
|
2011-12-14 21:52:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void tearDown() {
|
|
|
|
|
system.shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void scheduleOneOffTask() {
|
|
|
|
|
//#schedule-one-off-message
|
2012-09-26 10:56:25 +02:00
|
|
|
system.scheduler().scheduleOnce(Duration.create(50, TimeUnit.MILLISECONDS),
|
|
|
|
|
testActor, "foo", system.dispatcher());
|
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
|
2012-09-26 10:56:25 +02:00
|
|
|
ActorRef tickActor = system.actorOf(new Props().withCreator(
|
|
|
|
|
new UntypedActorFactory() {
|
|
|
|
|
public UntypedActor create() {
|
|
|
|
|
return new UntypedActor() {
|
|
|
|
|
public void onReceive(Object message) {
|
|
|
|
|
if (message.equals("Tick")) {
|
|
|
|
|
// Do someting
|
|
|
|
|
} else {
|
|
|
|
|
unhandled(message);
|
|
|
|
|
}
|
2011-12-14 21:52:39 +01:00
|
|
|
}
|
2012-09-26 10:56:25 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}));
|
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",
|
|
|
|
|
system.dispatcher());
|
2011-12-14 21:52:39 +01:00
|
|
|
|
|
|
|
|
//This cancels further Ticks to be sent
|
|
|
|
|
cancellable.cancel();
|
|
|
|
|
//#schedule-recurring
|
|
|
|
|
system.stop(tickActor);
|
|
|
|
|
}
|
|
|
|
|
}
|