Java Quartz scheduled task - prohibit simultaneous task execution

I use Quartz Job to perform certain tasks.

I also plan to execute it in my main application class, and what I'm trying to execute is not to allow concurrent instances of this job.

Therefore, the scheduler should only perform the task if its previous instance is complete.

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 main class application, I run 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 the scheduler not to start the second instance of MainJob if another one works ...

+6
source share
3 answers

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

See the official example or this tutorial on doing the job in parallel.

+13
source

You can implement StatefulJob or annotate DisallowConcurrentExecution

+2
source

@DissallowConcurrentExecution may do your work, but please think that this will only prevent your class from running twice on the same node.

Check out @ReneM's comment on Quartz 2.2 Planner and @DisallowConcurrentExecution

0
source

Source: https://habr.com/ru/post/983379/


All Articles