* previous `schedule` method is trying to maintain a fixed average frequency over time, but that can result in undesired bursts of scheduled tasks after a long GC or if the JVM process has been suspended, same with all other periodic scheduled message sending via various Timer APIs * most of the time "fixed delay" is more desirable * we can't just change because it's too big behavioral change and some might depend on previous behavior * deprecate the old `schedule` and introduce new `scheduleWithFixedDelay` and `scheduleAtFixedRate`, when fixing the deprecation warning users should make a concious decision of which behavior to use (scheduleWithFixedDelay in most cases) * Streams * SchedulerSpec * test both fixed delay and fixed rate * TimerSpec * FSM and PersistentFSM * mima * runnable as second parameter list, also in typed.Scheduler * IllegalStateException vs SchedulerException * deprecated annotations * api and reference docs, all places * migration guide
93 lines
2.4 KiB
Java
93 lines
2.4 KiB
Java
/*
|
|
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
|
|
*/
|
|
|
|
package jdocs.actor;
|
|
|
|
// #imports1
|
|
import java.time.Duration;
|
|
// #imports1
|
|
|
|
// #imports2
|
|
import akka.actor.Cancellable;
|
|
// #imports2
|
|
|
|
import jdocs.AbstractJavaTest;
|
|
import akka.actor.Props;
|
|
import akka.actor.AbstractActor;
|
|
import akka.actor.ActorRef;
|
|
import akka.actor.ActorSystem;
|
|
import akka.testkit.AkkaSpec;
|
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
|
import org.junit.*;
|
|
|
|
public class SchedulerDocTest extends AbstractJavaTest {
|
|
|
|
@ClassRule
|
|
public static AkkaJUnitActorSystemResource actorSystemResource =
|
|
new AkkaJUnitActorSystemResource("SchedulerDocTest", AkkaSpec.testConf());
|
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
|
private ActorRef testActor = system.actorOf(Props.create(MyActor.class));
|
|
|
|
@Test
|
|
public void scheduleOneOffTask() {
|
|
// #schedule-one-off-message
|
|
system
|
|
.scheduler()
|
|
.scheduleOnce(
|
|
Duration.ofMillis(50), testActor, "foo", system.dispatcher(), ActorRef.noSender());
|
|
// #schedule-one-off-message
|
|
|
|
// #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
|
|
}
|
|
|
|
@Test
|
|
public void scheduleRecurringTask() {
|
|
// #schedule-recurring
|
|
class Ticker extends AbstractActor {
|
|
@Override
|
|
public Receive createReceive() {
|
|
return receiveBuilder()
|
|
.matchEquals(
|
|
"Tick",
|
|
m -> {
|
|
// Do someting
|
|
})
|
|
.build();
|
|
}
|
|
}
|
|
|
|
ActorRef tickActor = system.actorOf(Props.create(Ticker.class, this));
|
|
|
|
// This will schedule to send the Tick-message
|
|
// to the tickActor after 0ms repeating every 50ms
|
|
Cancellable cancellable =
|
|
system
|
|
.scheduler()
|
|
.scheduleWithFixedDelay(
|
|
Duration.ZERO,
|
|
Duration.ofMillis(50),
|
|
tickActor,
|
|
"Tick",
|
|
system.dispatcher(),
|
|
ActorRef.noSender());
|
|
|
|
// This cancels further Ticks to be sent
|
|
cancellable.cancel();
|
|
// #schedule-recurring
|
|
system.stop(tickActor);
|
|
}
|
|
}
|