2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
|
2011-12-19 11:07:59 +01:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.actor;
|
2011-12-14 21:52:39 +01:00
|
|
|
|
|
|
|
|
//#imports1
|
|
|
|
|
import akka.actor.Props;
|
2017-03-16 09:30:00 +01:00
|
|
|
import jdocs.AbstractJavaTest;
|
2018-04-11 16:47:36 +02:00
|
|
|
import java.time.Duration;
|
2011-12-14 21:52:39 +01:00
|
|
|
//#imports1
|
|
|
|
|
|
|
|
|
|
//#imports2
|
|
|
|
|
import akka.actor.Cancellable;
|
|
|
|
|
//#imports2
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
import akka.actor.AbstractActor;
|
2011-12-14 21:52:39 +01:00
|
|
|
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
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
public class SchedulerDocTest extends AbstractJavaTest {
|
2016-05-30 12:54:27 +02:00
|
|
|
|
|
|
|
|
@ClassRule
|
|
|
|
|
public static 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();
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
private ActorRef testActor = system.actorOf(Props.create(MyActor.class));
|
2011-12-14 21:52:39 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void scheduleOneOffTask() {
|
|
|
|
|
//#schedule-one-off-message
|
2018-04-11 16:47:36 +02:00
|
|
|
system.scheduler().scheduleOnce(Duration.ofMillis(50),
|
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
|
2018-04-11 16:47:36 +02:00
|
|
|
system.scheduler().scheduleOnce(Duration.ofMillis(50),
|
2012-09-26 10:56:25 +02:00
|
|
|
new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2013-06-05 16:59:25 +02:00
|
|
|
testActor.tell(System.currentTimeMillis(), ActorRef.noSender());
|
2012-09-26 10:56:25 +02:00
|
|
|
}
|
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
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
class Ticker extends AbstractActor {
|
2013-04-14 22:56:41 +02:00
|
|
|
@Override
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.matchEquals("Tick", m -> {
|
|
|
|
|
// Do someting
|
|
|
|
|
})
|
|
|
|
|
.build();
|
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
|
2018-04-11 16:47:36 +02:00
|
|
|
Cancellable cancellable = system.scheduler().schedule(Duration.ZERO,
|
|
|
|
|
Duration.ofMillis(50), 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);
|
|
|
|
|
}
|
|
|
|
|
}
|