Java Quartz预定作业 - 禁止并发执行Job [英] Java Quartz scheduled Job - disallow concurrent execution of Job

查看:1028
本文介绍了Java Quartz预定作业 - 禁止并发执行Job的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Quartz Job执行特定任务。

I am using a Quartz Job for executing specific tasks.

我也在我的主应用程序类中安排它的执行,我想要完成的是不允许同时执行这个作业的实例。

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.

因此调度程序只应在前一个实例完成时执行该作业。

So the scheduler should only execute the job if its previous instance is finished.

这是我的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 .... <<<<<<<<<<<<<<<<<<<<");

    }
}

所以在我的应用程序的主类我我正在启动调度程序:

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);

}

我想阻止调度程序启动第二个MainJob实例如果另一个还在运行......

I would like to prevent scheduler from starting a second MainJob instance if another one is still running ...

推荐答案

只需使用 @DisallowConcurrentExecution Job类顶部的注释。

Just use the @DisallowConcurrentExecution Annotation on top of the Job class.

查看此官方文章示例或此教程

这篇关于Java Quartz预定作业 - 禁止并发执行Job的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆