Java Quartz 调度作业 - 禁止并发执行作业
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28879397/
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
Java Quartz scheduled Job - disallow concurrent execution of Job
提问by thanili
I am using a Quartz Job for executing specific tasks.
我正在使用 Quartz 作业来执行特定任务。
I am also scheduling its execution in my Main application class and what i am trying to accomplish is not to allow simultaneous instances of this job to be executed.
我还在我的 Main 应用程序类中安排它的执行,我试图完成的是不允许同时执行此作业的实例。
So the scheduler should only execute the job if its previous instance is finished.
因此,调度程序应该只在其前一个实例完成后才执行该作业。
Here is my Job class:
这是我的工作类:
public class MainJob implements Job {
static Logger log = Logger.getLogger(MainJob.class.getName());
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
GlobalConfig cfg = new GlobalConfig();
ProcessDicomFiles processDicomFiles = new ProcessDicomFiles();
ProcessPdfReportFiles processPdf = new ProcessPdfReportFiles();
try {
log.info("1. ---- SCHEDULED JOB -- setStudiesReadyToProcess");
processDicomFiles.setStudiesReadyToProcess();
log.info("2. ---- SCHEDULED JOB --- distributeToStudies");
processDicomFiles.distributeToStudies(cfg.getAssocDir());
...
//process any incoming PDF file
log.info("11. ---- SCHEDULED JOB --- processPdfFolder");
processPdf.processPdfFolder();
} catch (Exception ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.ERROR, null, ex);
}
log.info(">>>>>>>>>>> Scheduled Job has ended .... <<<<<<<<<<<<<<<<<<<<");
}
}
So in my application's main class i am starting the scheduler:
所以在我的应用程序的主类中,我正在启动调度程序:
...
//start Scheduler
try {
startScheduler();
} catch (SchedulerException ex) {
log.log(Level.INFO, null, ex);
}
...
public void startScheduler () throws SchedulerException {
//Creating scheduler factory and scheduler
factory = new StdSchedulerFactory();
scheduler = factory.getScheduler();
schedulerTimeWindow = config.getSchedulerTimeWindow();
JobDetailImpl jobDetail = new JobDetailImpl();
jobDetail.setName("First Job");
jobDetail.setJobClass(MainJob.class);
SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl();
simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
simpleTrigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
simpleTrigger.setRepeatInterval(schedulerTimeWindow);
simpleTrigger.setName("FirstTrigger");
//Start scheduler
scheduler.start();
scheduler.scheduleJob(jobDetail,simpleTrigger);
}
I would like to prevent scheduler from starting a second MainJob instance if another one is still running ...
如果另一个 MainJob 实例仍在运行,我想阻止调度程序启动第二个 MainJob 实例......
采纳答案by zerologiko
回答by Matt Stephenson
You can implement StatefulJobor annotate DisallowConcurrentExecution
回答by Alireza Fattahi
@DisallowConcurrentExecution
can do your job but please consider that it would only prevent your class from being run twice on the same node.
@DisallowConcurrentExecution
可以完成您的工作,但请注意它只会阻止您的课程在同一节点上运行两次。
Please see @ReneM comment in Quartz 2.2 multi scheduler and @DisallowConcurrentExecution
请参阅Quartz 2.2 multi scheduler 和 @DisallowConcurrentExecution 中的@ReneM 注释