我正在使用Quartz Job执行特定任务。
我也在我的主应用程序类中安排它的执行,我想要完成的是不允许同时执行这个作业的实例。
因此,调度程序只应在前一个实例完成时才执行该作业。
这是我的Job类:
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 .... <<<<<<<<<<<<<<<<<<<<");
}
}
所以在我的应用程序的主类中,我正在启动调度程序:
...
//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);
}
我想阻止调度程序启动第二个MainJob实例,如果另一个仍在运行...
答案 0 :(得分:22)
答案 1 :(得分:4)
答案 2 :(得分:1)
@DisallowConcurrentExecution
可以完成您的工作但请注意,它只会阻止您的课程在同一节点上运行两次。
请参阅Quartz 2.2 multi scheduler and @DisallowConcurrentExecution
中的@ReneM评论