2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2017-2019 Lightbend Inc. <https://www.lightbend.com>
|
2017-06-16 11:31:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package jdocs.actor;
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #timers
|
2018-04-11 16:47:36 +02:00
|
|
|
import java.time.Duration;
|
2017-06-16 11:31:00 +02:00
|
|
|
import akka.actor.AbstractActorWithTimers;
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #timers
|
2017-06-16 11:31:00 +02:00
|
|
|
|
|
|
|
|
public class TimerDocTest {
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
public
|
|
|
|
|
// #timers
|
|
|
|
|
static class MyActor extends AbstractActorWithTimers {
|
|
|
|
|
|
2017-06-16 11:31:00 +02:00
|
|
|
private static Object TICK_KEY = "TickKey";
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
|
|
|
private static final class FirstTick {}
|
|
|
|
|
|
|
|
|
|
private static final class Tick {}
|
2017-06-16 11:31:00 +02:00
|
|
|
|
|
|
|
|
public MyActor() {
|
2018-04-11 16:47:36 +02:00
|
|
|
getTimers().startSingleTimer(TICK_KEY, new FirstTick(), Duration.ofMillis(500));
|
2017-06-16 11:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
.match(
|
|
|
|
|
FirstTick.class,
|
|
|
|
|
message -> {
|
|
|
|
|
// do something useful here
|
2019-05-27 11:53:26 +02:00
|
|
|
getTimers().startTimerWithFixedDelay(TICK_KEY, new Tick(), Duration.ofSeconds(1));
|
2019-01-12 04:00:53 +08:00
|
|
|
})
|
|
|
|
|
.match(
|
|
|
|
|
Tick.class,
|
|
|
|
|
message -> {
|
|
|
|
|
// do something useful here
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2017-06-16 11:31:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #timers
|
2017-06-16 11:31:00 +02:00
|
|
|
}
|