java 如何在石英调度程序中暂停作业?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7751973/
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 suspend job in quartz scheduler?
提问by Hemant Metalia
Hi i am creating an application that executes the method of a class based on a cron expression. For that i am using spring quartz in which i have to configure all these stuffs in my spring-file it works fine and jobs are executing based on the cron expression, But now i want to suspend the next execution of particular job in java class based on the choice of a user from UI. Then is there any way to do this ??
嗨,我正在创建一个基于 cron 表达式执行类方法的应用程序。为此,我正在使用 spring 石英,其中我必须在我的 spring 文件中配置所有这些东西,它工作正常,并且作业正在根据 cron 表达式执行,但现在我想在基于 java 类的特定作业中暂停下一次执行关于用户从 UI 中的选择。那有没有办法做到这一点??
can i get the details of all running job it the context ? if so then i can filter the jobs and try to suspend that job for next execution.
我可以在上下文中获取所有正在运行的作业的详细信息吗?如果是这样,那么我可以过滤作业并尝试暂停该作业以供下次执行。
回答by Anthony Accioly
Inject your SchedulerFactoryBean. Use it's getScheduler
method to obtain a quartz Schedulerand use rescheduleJobor other methods from quartz API to perform your task.
注入您的SchedulerFactoryBean。使用它的getScheduler
方法来获取一个quartz Scheduler并使用rescheduleJob或来自quartz API 的其他方法来执行您的任务。
回答by Hemant Metalia
I got it work by use of following code
我使用以下代码让它工作
stdScheduler is a scheduleFactoryBean
stdScheduler 是一个 scheduleFactoryBean
String groupnames[] = stdScheduler.getJobGroupNames();
for (String groupName : groupnames) {
String[] jobNames = stdScheduler.getJobNames(groupName);
for (String jobName : jobNames) {
if (jobName.equals("jobName")) {
stdScheduler.pauseJob(jobName, groupName);
}
}
}
回答by Hemant Metalia
We can use the stdScheduler.pauseJob(JobKey jobKey) method to reduce the number of loops mentioned in the code above
我们可以使用 stdScheduler.pauseJob(JobKey jobKey) 方法来减少上面代码中提到的循环次数
回答by k_o_
If you got hold of the SchedulerFactoryBean by injection or somehow else there are the convenience methods:
如果您通过注入或其他方式获得了 SchedulerFactoryBean,则有一些方便的方法:
schedulerFactoryBean.getScheduler().standby();
schedulerFactoryBean.getScheduler().start();
When using Quartz directly also this works:
当直接使用 Quartz 时,这也有效:
@Autowired
private Scheduler scheduler;
...
scheduler.standby();
...
scheduler.start();