thanili thanili - 1 year ago 169
Java Question

Java Quartz scheduled Job - disallow concurrent execution of 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 simoultaneous instances of this job to be executed.

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

Here is my Job class:

public class MainJob implements Job {

static Logger log = Logger.getLogger(MainJob.class.getName());

public void execute(JobExecutionContext arg0) throws JobExecutionException {

GlobalConfig cfg = new GlobalConfig();

ProcessDicomFiles processDicomFiles = new ProcessDicomFiles();
ProcessPdfReportFiles processPdf = new ProcessPdfReportFiles();

try {"1. ---- SCHEDULED JOB -- setStudiesReadyToProcess");
processDicomFiles.setStudiesReadyToProcess();"2. ---- SCHEDULED JOB --- distributeToStudies");


//process any incoming PDF file"11. ---- SCHEDULED JOB --- processPdfFolder");

} catch (Exception ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.ERROR, null, ex);
}">>>>>>>>>>> Scheduled Job has ended .... <<<<<<<<<<<<<<<<<<<<");



So in my application's main class i am starting the scheduler:

//start Scheduler
try {
} 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");

SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl();
simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000));

//Start scheduler


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

Answer Source

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

See this official example or this tutorial about concurrent job execution.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download