Search code examples
javascalaplayframeworkakkascheduled-tasks

How to stop all scheudlers all at time in java play


scheduledFeeds.forEach(scheduledFeed -> {
         actorSystem.scheduler().schedule(
                Duration.create(nextExecutionTime, TimeUnit.SECONDS), // initialDelay
                Duration.create(interval, timeUnit), // interval
                () -> runJob(tenant),
                this.executionContext )

       });

I need to stop all jobs at once do I need to store all the cancellables and then cancel ?If ,yes then how to store and cancel?


Solution

  • When you schedule the task, store that object in a Collection so that they can be cancelled later if needed.

    Collection<Future<?>> futures = ...;
    
    futures.add(actorSystem.scheduler().schedule(...));
    

    To cancel

    futures.forEach(future -> future.cancel(true));
    

    There might be an exception there IIRC that might need to be caught.