计划任务的顺序执行[Quartz Scheduler v.2.1.6]

使用Quartz做计划任务时,默认情况下,当前任务总会执行,无论前一个任务是否结束。从如下日志可以看出,job被并发执行,无视前面任务是否结束。

 

13:14:04,095 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_QuartzSchedulerThread - new a simple job
13:14:04,095 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-1 - n=1
13:14:04,095 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-1 - group1.job1 executing at Sun Apr 07 13:14:04 CST 2013
13:14:06,001 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_QuartzSchedulerThread - new a simple job
13:14:06,001 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-2 - n=2
13:14:06,001 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-2 - group1.job1 executing at Sun Apr 07 13:14:06 CST 2013
13:14:07,095 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-1 - group1.job1 done.
13:14:08,001 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_QuartzSchedulerThread - new a simple job
13:14:08,001 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-3 - n=3
13:14:08,001 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-3 - group1.job1 executing at Sun Apr 07 13:14:08 CST 2013
13:14:09,001 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-2 - group1.job1 done.

 

 

因此,如果采用默认条件,需要考虑并发执行的逻辑问题,否则需要设置为顺序执行。

 

第一种办法是为自定义的job加上@org.quartz.DisallowConcurrentExecution注解,告诉quartz不允许并发执行job任务。这个办法粗粒度地解决了并发问题,缺少灵活度,但很简单,也很简洁。

 

13:20:04,017 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_QuartzSchedulerThread - new a simple job
13:20:04,017 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-1 - n=1
13:20:04,017 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-1 - group1.job1 executing at Sun Apr 07 13:20:04 CST 2013
13:20:07,017 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-1 - group1.job1 done.
13:20:07,017 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_QuartzSchedulerThread - new a simple job
13:20:07,017 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-2 - n=2
13:20:07,017 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-2 - group1.job1 executing at Sun Apr 07 13:20:07 CST 2013
13:20:10,017 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-2 - group1.job1 done.

 

 

第二种办法是在job的执行方法体内编写处理代码

 

13:24:18,236 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_QuartzSchedulerThread - new a simple job
13:24:18,236 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-1 - n=1
13:24:18,236 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-1 - group1.job1 executing at Sun Apr 07 13:24:18 CST 2013
13:24:20,001 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_QuartzSchedulerThread - new a simple job
13:24:20,001 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-2 - < 
13:24:21,236 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-1 - group1.job1 done.
13:24:22,001 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_QuartzSchedulerThread - new a simple job
13:24:22,001 INFO  [creative.air.schedule.quartz.SimpleJob] DefaultQuartzScheduler_Worker-3 - n=2

 

相关测试代码:

 

测试类

job生产类

job类

 

最后需要注意的是,quartz的发展出现了分支,目前存在并行版本

spring与quartz1集成更友好

计划任务的顺序执行[Quartz Scheduler v.2.1.6]_第1张图片

你可能感兴趣的:(scheduler)