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
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #imports1
|
2018-04-11 16:47:36 +02:00
|
|
|
import java.time.Duration;
|
2019-01-12 04:00:53 +08:00
|
|
|
// #imports1
|
2011-12-14 21:52:39 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #imports2
|
2011-12-14 21:52:39 +01:00
|
|
|
import akka.actor.Cancellable;
|
2019-01-12 04:00:53 +08:00
|
|
|
// #imports2
|
2011-12-14 21:52:39 +01:00
|
|
|
|
2019-05-27 11:53:26 +02:00
|
|
|
import jdocs.AbstractJavaTest;
|
|
|
|
|
import akka.actor.Props;
|
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 {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2016-05-30 12:54:27 +02:00
|
|
|
@ClassRule
|
2019-01-12 04:00:53 +08:00
|
|
|
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() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #schedule-one-off-message
|
|
|
|
|
system
|
|
|
|
|
.scheduler()
|
2019-05-27 11:53:26 +02:00
|
|
|
.scheduleOnce(
|
|
|
|
|
Duration.ofMillis(50), testActor, "foo", system.dispatcher(), ActorRef.noSender());
|
2019-01-12 04:00:53 +08:00
|
|
|
// #schedule-one-off-message
|
2011-12-14 21:52:39 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #schedule-one-off-thunk
|
|
|
|
|
system
|
|
|
|
|
.scheduler()
|
|
|
|
|
.scheduleOnce(
|
|
|
|
|
Duration.ofMillis(50),
|
|
|
|
|
new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
testActor.tell(System.currentTimeMillis(), ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
system.dispatcher());
|
|
|
|
|
// #schedule-one-off-thunk
|
2011-12-14 21:52:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void scheduleRecurringTask() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #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()
|
2019-01-12 04:00:53 +08:00
|
|
|
.matchEquals(
|
|
|
|
|
"Tick",
|
|
|
|
|
m -> {
|
|
|
|
|
// Do someting
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2013-04-14 22:56:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08: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
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// This will schedule to send the Tick-message
|
|
|
|
|
// to the tickActor after 0ms repeating every 50ms
|
|
|
|
|
Cancellable cancellable =
|
|
|
|
|
system
|
|
|
|
|
.scheduler()
|
2019-05-27 11:53:26 +02:00
|
|
|
.scheduleWithFixedDelay(
|
|
|
|
|
Duration.ZERO,
|
|
|
|
|
Duration.ofMillis(50),
|
|
|
|
|
tickActor,
|
|
|
|
|
"Tick",
|
|
|
|
|
system.dispatcher(),
|
|
|
|
|
ActorRef.noSender());
|
2011-12-14 21:52:39 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// This cancels further Ticks to be sent
|
2011-12-14 21:52:39 +01:00
|
|
|
cancellable.cancel();
|
2019-01-12 04:00:53 +08:00
|
|
|
// #schedule-recurring
|
2011-12-14 21:52:39 +01:00
|
|
|
system.stop(tickActor);
|
|
|
|
|
}
|
|
|
|
|
}
|