Search in sources :

Example 1 with Scheduler

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();
        }
    };
}
Also used : Scheduler(akka.actor.Scheduler) Cancellable(akka.actor.Cancellable) ExecutionContextExecutor(scala.concurrent.ExecutionContextExecutor)

Aggregations

Cancellable (akka.actor.Cancellable)1 Scheduler (akka.actor.Scheduler)1 ExecutionContextExecutor (scala.concurrent.ExecutionContextExecutor)1