How to cancel Spring timer execution

I need to cancel Spring timer execution, or at least change the execution frequency based on some conditions. Used both org.springframework.scheduling.quartz.SimpleTriggerBean and org.springframework.scheduling.timer.ScheduledTimerTask . Unable to find a way to do this.

+7
source share
3 answers

NOTE : this is for Spring 3.0+

  • Read Spring Planning Documentation

  • Use the TaskScheduler service, such as TimerManagerTaskScheduler or ThreadPoolTaskScheduler .

  • Schedule your task by calling the TaskScheduler.schedule*() method and save the returned ScheduledFuture .

  • If you want to cancel execution, call ScheduledFuture.cancel() . This will stop further challenges to your task. At this time, you can reschedule if you want by calling TaskScheduler.schedule*() for your task with different parameters.

+14
source

This is far from the best solution, but if you cannot think of anything else, you can always use a boolean that will be checked every time the event is fired, and if the boolean is false, the timertask trigger method should stop immediately.

0
source

The solution is to assign an id to org.springframework.scheduling.timer.TimerFactoryBean, and then extract this bean from the application, apply it to the timer and the call cancellation method for this object.

0
source

All Articles