Lce Man
Lce Man

Reputation: 59

why quartz scheduleJob() execute many times?

public class QuartzStudy implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) {
    System.out.println(jobExecutionContext.getJobDetail().getKey().getName() + "-" + Thread.currentThread().getName() + "-" + Thread.currentThread().getId() + "-" + new Date());
}

private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();
private static Scheduler scheduler;
static {
    try {
        scheduler = schedulerFactory.getScheduler();
        scheduler.start();
    } catch (SchedulerException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) throws SchedulerException, InterruptedException {
    JobDetail jobDetail = JobBuilder.newJob(QuartzStudy.class).withIdentity("job1").build();
    CronTrigger trigger = TriggerBuilder.newTrigger()
            .withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?").withMisfireHandlingInstructionIgnoreMisfires())
            .build();
    scheduler.scheduleJob(jobDetail, trigger);
    Thread.sleep(10000);
    scheduler.pauseJob(jobDetail.getKey());
}

}

This code should console twice and then pause?

But sometimes console three times,why?

job1-DefaultQuartzScheduler_Worker-1-14-Thu Jan 03 09:34:45 CST 2019

job1-DefaultQuartzScheduler_Worker-2-15-Thu Jan 03 09:34:50 CST 2019

job1-DefaultQuartzScheduler_Worker-3-16-Thu Jan 03 09:34:55 CST 2019

Upvotes: 0

Views: 533

Answers (1)

mukesh210
mukesh210

Reputation: 2892

First of all, */5 * * * * ? means Every 5 seconds starting at :00 second after the minute.

You are doing Thread.sleep(10000);. This means main thread will be paused for 10 seconds. But, since you are getting logs even in those 10 seconds, that means scheduler is independent of main thread. So, at 10th second, scheduler will create a thread to run execute and also after 10th second, you are pausing job.If first one(i.e. creating of thread occurs before pausing) occurs first, logs will be printed thrice. Whereas if second one(pausing of job occurs before spawning new thread) occurs first, logs will be printed only twice. That's why sometime log is getting printed twice and sometimes thrice.

So, to be sure that logs are printed only twice, decrease sleep time to 9 seconds. I tried below code and it always prints logs twice:

public static void main(String[] args) throws SchedulerException, InterruptedException {
        System.out.println("Main method:" + Thread.currentThread().getName() + "-" + Thread.currentThread().getId());
        JobDetail jobDetail = JobBuilder.newJob(QuartzStudy.class).withIdentity("job1").build();
        CronTrigger trigger = TriggerBuilder.newTrigger()
                .withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?").withMisfireHandlingInstructionIgnoreMisfires())
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();    // it's better to start job once you have scheduled one
        Thread.sleep(9000);
        scheduler.pauseJob(jobDetail.getKey());
        System.out.println("Job is Paused!!!");
    }

Upvotes: 1

Related Questions