Java 如何在 Spring 中取消预定的 Quartz 作业
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4269754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to cancel a scheduled Quartz job in Spring
提问by Steve Neal
I'm using Spring to inject a Quartz scheduler (abstracted with Spring's TaskScheduler interface) into my app that loads jobs configured from a database at startup.
我正在使用 Spring 将 Quartz 调度程序(使用 Spring 的 TaskScheduler 接口抽象)注入我的应用程序,该程序在启动时加载从数据库配置的作业。
It adds each job in the scheduler something like this:
它将每个作业添加到调度程序中,如下所示:
TaskScheduler taskScheduler = ...;//injected
Runnable runableThing = ...;
String cronExpression = ...; //from DB
taskScheduler.schedule(runableThing, new CronTrigger(cronExpression));
my question is this: Is it possible to specify something like a job_id that can subsequently be used to cancel the job/trigger - say in response to a user selecting the job to be cancelled in the web interface?
我的问题是:是否可以指定类似 job_id 之类的东西,随后可以用来取消作业/触发器 - 例如响应用户在 Web 界面中选择要取消的作业?
I've looked at the Spring docs and can't see a way to do this.
我查看了 Spring 文档,但看不到这样做的方法。
Any ideas gratefully received.
任何想法都非常感谢。
回答by Puspendu Banerjee
Unscheduling a Particular Trigger of Job
取消调度作业的特定触发器
scheduler.unscheduleJob(triggerName, triggerGroup);
Deleting a Job and Unscheduling All of Its Triggers
删除作业并取消调度其所有触发器
scheduler.deleteJob(jobName, jobGroup);
Ref: http://www.opensymphony.com/quartz/wikidocs/UnscheduleJob.html
参考:http: //www.opensymphony.com/quartz/wikidocs/UnscheduleJob.html
回答by Chris Dev
ScheduledFuture<V> job = taskSchedule.schedule(runableThing, new CronTrigger(cronExpression))
job.cancel(true);