How to cancel Spring timer execution

后端 未结 3 712
生来不讨喜
生来不讨喜 2021-01-02 06:26

I need to cancel Spring timer execution or at least change the execution frequency based on some conditions. Was using both org.springframework.scheduling.quartz.Simpl

3条回答
  •  猫巷女王i
    2021-01-02 06:32

    NOTE: This is for Spring 3.0+

    1. Read Spring documentation on scheduling tasks

    2. Use a TaskScheduler service, such as a TimerManagerTaskScheduler or ThreadPoolTaskScheduler.

    3. Schedule your task by calling some TaskScheduler.schedule*() method and store the returning ScheduledFuture.

    4. When you want to cancel execution, invoke ScheduledFuture.cancel(). That will stop further invocations of your task. At this time, you can reschedule if you want by calling TaskScheduler.schedule*() for your task with different parameters.

提交回复
热议问题