问题描述
我需要取消Spring定时器执行或至少根据某些条件更改执行频率。同时使用 org.springframework.scheduling.quartz.SimpleTriggerBean
和 org.springframework.scheduling.timer.ScheduledTimerTask
。找不到怎么做的方法。
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.SimpleTriggerBean
and org.springframework.scheduling.timer.ScheduledTimerTask
. Cannot find the way how to do it.
推荐答案
注意:这是适用于Spring 3.0+
NOTE: This is for Spring 3.0+
-
阅读
使用 TaskScheduler
服务,例如 TimerManagerTaskScheduler
或 ThreadPoolTaskScheduler
。
通过调用某些 TaskScheduler.schedule *()
方法安排您的任务并存储返回的 ScheduledFuture
。
Schedule your task by calling some TaskScheduler.schedule*()
method and store the returning ScheduledFuture
.
如果要取消执行,请调用 ScheduledFuture.cancel()
。这将停止进一步调用您的任务。此时,您可以通过调用 TaskScheduler.schedule *()
为您的任务使用不同的参数重新安排。
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.
这篇关于如何取消Spring计时器执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!