How to stop ActorSystem schedular which is scheduled with interval

42 Views Asked by At
Public boolean checkStatus(){
    if(case){
        return true;
    }
    return false;
}

final ActorSystem system = ActorSystem.apply();
Cancellable result = system.scheduler.schedule(Duration.create(delay,TimeUnit.MILLISECONDS),Duration.create(interval, TimeUnit.MILLISECONDS)(checkStatus());

How i can stop scheduler when checkStatus case is true.

1

There are 1 best solutions below

0
On

Instead of cancelling scheduled task you can use scheduleOnce method instead and then schedule it again if check is false or not if it's true. You will get the semantics you want that way.

Something along those lines:

public boolean checkStatus(){
    if(case){
        return true;
    }
    return false;
}

public rescheduleIfFalse(ActorSystem system) {
    if(!checkStatus) {
        system.scheduleOnce(Duration.create(interval, TimeUnit.MILLISECONDS))(rescheduleIfFalse);
    }
}

final ActorSystem system = ActorSystem.apply();
rescheduleIfFalse(system)