概述
使用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]的全部内容,希望文章能够帮你解决计划任务的顺序执行[Quartz Scheduler v.2.1.6]所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复