Java Quartz预定作业 – 禁止并发执行Job

前端之家收集整理的这篇文章主要介绍了Java Quartz预定作业 – 禁止并发执行Job前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用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,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实例,如果另一个仍在运行…

解决方法

只需在Job类的顶部使用@DisallowConcurrentExecution Annotation即可.

有关并发作业执行的信息,请参阅此官方example或此tutorial.

猜你在找的Java相关文章