* 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
47 lines
1 KiB
Java
47 lines
1 KiB
Java
/*
|
|
* Copyright (C) 2017-2019 Lightbend Inc. <https://www.lightbend.com>
|
|
*/
|
|
|
|
package jdocs.actor;
|
|
|
|
// #timers
|
|
import java.time.Duration;
|
|
import akka.actor.AbstractActorWithTimers;
|
|
|
|
// #timers
|
|
|
|
public class TimerDocTest {
|
|
|
|
public
|
|
// #timers
|
|
static class MyActor extends AbstractActorWithTimers {
|
|
|
|
private static Object TICK_KEY = "TickKey";
|
|
|
|
private static final class FirstTick {}
|
|
|
|
private static final class Tick {}
|
|
|
|
public MyActor() {
|
|
getTimers().startSingleTimer(TICK_KEY, new FirstTick(), Duration.ofMillis(500));
|
|
}
|
|
|
|
@Override
|
|
public Receive createReceive() {
|
|
return receiveBuilder()
|
|
.match(
|
|
FirstTick.class,
|
|
message -> {
|
|
// do something useful here
|
|
getTimers().startTimerWithFixedDelay(TICK_KEY, new Tick(), Duration.ofSeconds(1));
|
|
})
|
|
.match(
|
|
Tick.class,
|
|
message -> {
|
|
// do something useful here
|
|
})
|
|
.build();
|
|
}
|
|
}
|
|
// #timers
|
|
}
|