JJ Redikes
JJ Redikes

Reputation: 431

How to cancel all the scheduled tasks with TaskScheduler object?

Hey I'm using with TaskScheduler object to schedule a few task in a different time.

I can't find the way to stop all the tasks, the only way I found is to restart my server(application) and I don't want to do it.

I have been trying to set the executer to null but it didn't work.

I'm attaching my code:

    private TaskScheduler executor;

    public void createSched() {
    Runnable task;
    task=()-> LOGGER.info("hello");
    TimeZone time=TimeZone.getTimeZone("UTC");
    executor.schedule(task, new CronTrigger("0,5,10,15,20,25,30 * * * * ?",time));
   // this.executor=null;
}

after the code above the tasks keep going and I cant stop it anymore...

Upvotes: 2

Views: 523

Answers (1)

Tal Shani
Tal Shani

Reputation: 690

Try this link

What you need to do is to keep a hash map of all the tasks from ScheduledFuture type and then you will be able to cancel the task.

Hope it helped!

Upvotes: 1

Related Questions