gpt4 book ai didi

Java Quartz 计划作业 - 不允许并发执行作业

转载 作者:行者123 更新时间:2023-12-02 01:03:09 25 4
gpt4 key购买 nike

我正在使用 Quartz 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 实例仍在运行,我想阻止调度程序启动第二个 MainJob 实例...

最佳答案

只需在 Job 类之上使用 @DisallowConcurrentExecution 注释即可。

看这个官方example或者这个tutorial关于并发作业执行。

关于Java Quartz 计划作业 - 不允许并发执行作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57755255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com