2017-06-16 11:31:00 +02:00
|
|
|
/**
|
2018-01-04 17:26:29 +00:00
|
|
|
* Copyright (C) 2017-2018 Lightbend Inc. <https://www.lightbend.com>
|
2017-06-16 11:31:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package jdocs.actor;
|
|
|
|
|
|
|
|
|
|
//#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;
|
|
|
|
|
|
|
|
|
|
//#timers
|
|
|
|
|
|
|
|
|
|
public class TimerDocTest {
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
//#timers
|
|
|
|
|
public class MyActor extends AbstractActorWithTimers {
|
|
|
|
|
|
|
|
|
|
private static Object TICK_KEY = "TickKey";
|
|
|
|
|
private static final class FirstTick {
|
|
|
|
|
}
|
|
|
|
|
private static final class Tick {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
.match(FirstTick.class, message -> {
|
|
|
|
|
// do something useful here
|
2018-04-11 16:47:36 +02:00
|
|
|
getTimers().startPeriodicTimer(TICK_KEY, new Tick(), Duration.ofSeconds(1));
|
2017-06-16 11:31:00 +02:00
|
|
|
})
|
|
|
|
|
.match(Tick.class, message -> {
|
|
|
|
|
// do something useful here
|
|
|
|
|
})
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#timers
|
|
|
|
|
}
|