use of akka.actor.Scheduler in project SSM by Intel-bigdata.
the class AgentUtils method repeatActionUntil.
public static Cancellable repeatActionUntil(ActorSystem system, FiniteDuration initialDelay, FiniteDuration interval, FiniteDuration timeout, Runnable action, Runnable onTimeout) {
final Scheduler scheduler = system.scheduler();
final ExecutionContextExecutor dispatcher = system.dispatcher();
final Cancellable run = scheduler.schedule(initialDelay, interval, action, dispatcher);
final Cancellable cancelRun = scheduler.scheduleOnce(timeout, new Runnable() {
@Override
public void run() {
run.cancel();
}
}, dispatcher);
final Cancellable fail = scheduler.scheduleOnce(timeout, onTimeout, dispatcher);
return new Cancellable() {
@Override
public boolean cancel() {
return run.cancel() && cancelRun.cancel() && fail.cancel();
}
@Override
public boolean isCancelled() {
return run.isCancelled() && cancelRun.isCancelled() && fail.isCancelled();
}
};
}
Aggregations