关于quartz job的类型、动态设定Trigger参数,网上有很多文章了。这篇文章主要介绍的是我在实际实现过程中的遇到的一些细节上的问题:
一 JOB State
在通过MethodInvokingJobDetailFactoryBean在运行中动态生成的Job,配置的xml文件有个concurrent属性,表示job是否可以并行运行:如果一个job的业务处理发费的时间超过了job的启动的间隔时间(repeatInterval),这个属性非常有用。如果为false,那么,在这种情况下,当前job还在运行,那么下一个job只能延时运行。如果为true,那么job就会并行运行。在实际的应用中应该配置为true/false,要根据需要了(废话)。
如果通过继承QuartzJobBean实现job的话,默认情况下QuartzJobBean是implements org.quartz.Job接口的,也就是说job示例是stateless的,会出现前面所述的并行情况。而代码中却要求job任务必需串行,解决办法:在job子类中继续implements org.quartz.StatefulJob。那么这个job实例变成了Stateful,job任务也就是串行的了。
注:
在Quartz中,如果实现org.quartz.Job接口,那么这个job是stateless的,job实例的参数不能在多个任务之间共享,如果实现org.quartz.StatefulJob,这个job是个单例的,job实例的属性可以从当前任务传递到下一个任务。
二 动态设定Trigger参数
这篇文章已经很详细了:
http://hi.baidu.com/vip099/blog/item/51bbb03d555f5702bba16766.html
我在动态设定Spring Quartz Trigger:SimpleTriggerBean、CronTriggerBean时,如果Job的state不同,结果也是不同的:
并行设置:concurrent true
0 2009-07-23 14:17:35,328 [main] INFO FileSystemXmlApplicationContext - Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1c5c1: display name [org.springframework.context.support.FileSystemXmlApplicationContext@1c5c1]; startup date [Thu Jul 23 14:17:35 CST 2009]; root of context hierarchy
109 2009-07-23 14:17:35,437 [main] INFO XmlBeanDefinitionReader - Loading XML bean definitions from file [D:/J2EE/IDE/workspace/springTask/org/springtask/quartzjob/changable/simpletrigger/applicationContext.xml]
328 2009-07-23 14:17:35,656 [main] INFO FileSystemXmlApplicationContext - Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@1c5c1]: org.springframework.beans.factory.support.DefaultListableBeanFactory@126e85f
359 2009-07-23 14:17:35,687 [main] INFO DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@126e85f: defining beans [jobManager,jobData,demoJob,jobDetailBean,simpleTriggerBean,timerFactoryBean]; root of factory hierarchy
437 2009-07-23 14:17:35,765 [main] INFO DemoJob - set this.jobData
*************
547 2009-07-23 14:17:35,875 [main] INFO SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
547 2009-07-23 14:17:35,875 [main] INFO QuartzScheduler - Quartz Scheduler v.1.6.5 created.
547 2009-07-23 14:17:35,875 [main] INFO RAMJobStore - RAMJobStore initialized.
547 2009-07-23 14:17:35,875 [main] INFO StdSchedulerFactory - Quartz scheduler 'QuartzScheduler' initialized from an externally provided properties instance.
547 2009-07-23 14:17:35,875 [main] INFO StdSchedulerFactory - Quartz scheduler version: 1.6.5
562 2009-07-23 14:17:35,890 [main] INFO QuartzScheduler - JobFactory set to: org.springframework.scheduling.quartz.AdaptableJobFactory@16fe0f4
562 2009-07-23 14:17:35,890 [main] INFO SchedulerFactoryBean - Starting Quartz Scheduler now
562 2009-07-23 14:17:35,890 [main] INFO QuartzScheduler - Scheduler QuartzScheduler_$_NON_CLUSTERED started.
重设时间?(y/n)
10933534 Job is started at 2009-17-23:02/17/38
3500 2009-07-23 14:17:38,828 [QuartzScheduler_Worker-1] INFO DemoJob - 10933534 org.springtask.quartzjob.changable.simpletrigger.DemoJob is sleeping
13500 2009-07-23 14:17:48,828 [QuartzScheduler_Worker-1] INFO DemoJob - 10933534 sleep 10000
13500 2009-07-23 14:17:48,828 [QuartzScheduler_Worker-1] INFO DemoJob - 10933534 org.springtask.quartzjob.changable.simpletrigger.DemoJob is done
10933534 Job is started at 2009-17-23:02/17/49
14484 2009-07-23 14:17:49,812 [QuartzScheduler_Worker-2] INFO DemoJob - 10933534 org.springtask.quartzjob.changable.simpletrigger.DemoJob is sleeping
y
reschedule begin...
reschedule end...
10933534 Job is started at 2009-17-23:02/17/53
18484 2009-07-23 14:17:53,812 [QuartzScheduler_Worker-3] INFO DemoJob - 10933534 org.springtask.quartzjob.changable.simpletrigger.DemoJob is sleeping
24484 2009-07-23 14:17:59,812 [QuartzScheduler_Worker-2] INFO DemoJob - 10933534 sleep 10000
24484 2009-07-23 14:17:59,812 [QuartzScheduler_Worker-2] INFO DemoJob - 10933534 org.springtask.quartzjob.changable.simpletrigger.DemoJob is done
28484 2009-07-23 14:18:03,812 [QuartzScheduler_Worker-3] INFO DemoJob - 10933534 sleep 10000
28484 2009-07-23 14:18:03,812 [QuartzScheduler_Worker-3] INFO DemoJob - 10933534 org.springtask.quartzjob.changable.simpletrigger.DemoJob is done
10933534 Job is started at 2009-18-23:02/18/08
33484 2009-07-23 14:18:08,812 [QuartzScheduler_Worker-4] INFO DemoJob - 10933534 org.springtask.quartzjob.changable.simpletrigger.DemoJob is sleeping
43484 2009-07-23 14:18:18,812 [QuartzScheduler_Worker-4] INFO DemoJob - 10933534 sleep 10000
43484 2009-07-23 14:18:18,812 [QuartzScheduler_Worker-4] INFO DemoJob - 10933534 org.springtask.quartzjob.changable.simpletrigger.DemoJob is done
串行设置:concurrent false
BODY { FONT-FAMILY:Tahoma; FONT-SIZE:10pt }P { FONT-FAMILY:Tahoma; FONT-SIZE:10pt }DIV { FONT-FAMILY:Tahoma; FONT-SIZE:10pt }TD { FONT-FAMILY:Tahoma; FONT-SIZE:10pt }
0 2009-07-23 14:11:33,093 [main] INFO FileSystemXmlApplicationContext - Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1c5c1: display name [org.springframework.context.support.FileSystemXmlApplicationContext@1c5c1]; startup date [Thu Jul 23 14:11:33 CST 2009]; root of context hierarchy
110 2009-07-23 14:11:33,203 [main] INFO XmlBeanDefinitionReader - Loading XML bean definitions from file [D:/J2EE/IDE/workspace/springTask/org/springtask/quartzjob/changable/simpletrigger/applicationContext.xml]
313 2009-07-23 14:11:33,406 [main] INFO FileSystemXmlApplicationContext - Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@1c5c1]: org.springframework.beans.factory.support.DefaultListableBeanFactory@126e85f
344 2009-07-23 14:11:33,437 [main] INFO DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@126e85f: defining beans [jobManager,jobData,demoJob,jobDetailBean,simpleTriggerBean,timerFactoryBean]; root of factory hierarchy
407 2009-07-23 14:11:33,500 [main] INFO DemoJob - set this.jobData
*************
532 2009-07-23 14:11:33,625 [main] INFO SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
532 2009-07-23 14:11:33,625 [main] INFO QuartzScheduler - Quartz Scheduler v.1.6.5 created.
532 2009-07-23 14:11:33,625 [main] INFO RAMJobStore - RAMJobStore initialized.
532 2009-07-23 14:11:33,625 [main] INFO StdSchedulerFactory - Quartz scheduler 'QuartzScheduler' initialized from an externally provided properties instance.
532 2009-07-23 14:11:33,625 [main] INFO StdSchedulerFactory - Quartz scheduler version: 1.6.5
532 2009-07-23 14:11:33,625 [main] INFO QuartzScheduler - JobFactory set to: org.springframework.scheduling.quartz.AdaptableJobFactory@d02b51
532 2009-07-23 14:11:33,625 [main] INFO SchedulerFactoryBean - Starting Quartz Scheduler now
532 2009-07-23 14:11:33,625 [main] INFO QuartzScheduler - Scheduler QuartzScheduler_$_NON_CLUSTERED started.
重设时间?(y/n)
4916061 Job is started at 2009-11-23:02/11/36
3532 2009-07-23 14:11:36,625 [QuartzScheduler_Worker-1] INFO DemoJob - 4916061 org.springtask.quartzjob.changable.simpletrigger.DemoJob is sleeping
13532 2009-07-23 14:11:46,625 [QuartzScheduler_Worker-1] INFO DemoJob - 4916061 sleep 10000
13532 2009-07-23 14:11:46,625 [QuartzScheduler_Worker-1] INFO DemoJob - 4916061 org.springtask.quartzjob.changable.simpletrigger.DemoJob is done
4916061 Job is started at 2009-11-23:02/11/47
14469 2009-07-23 14:11:47,562 [QuartzScheduler_Worker-2] INFO DemoJob - 4916061 org.springtask.quartzjob.changable.simpletrigger.DemoJob is sleeping
24469 2009-07-23 14:11:57,562 [QuartzScheduler_Worker-2] INFO DemoJob - 4916061 sleep 10000
24469 2009-07-23 14:11:57,562 [QuartzScheduler_Worker-2] INFO DemoJob - 4916061 org.springtask.quartzjob.changable.simpletrigger.DemoJob is done
4916061 Job is started at 2009-11-23:02/11/58
25469 2009-07-23 14:11:58,562 [QuartzScheduler_Worker-3] INFO DemoJob - 4916061 org.springtask.quartzjob.changable.simpletrigger.DemoJob is sleeping
y
reschedule begin...
reschedule end...
35469 2009-07-23 14:12:08,562 [QuartzScheduler_Worker-3] INFO DemoJob - 4916061 sleep 10000
35469 2009-07-23 14:12:08,562 [QuartzScheduler_Worker-3] INFO DemoJob - 4916061 org.springtask.quartzjob.changable.simpletrigger.DemoJob is done
4916061 Job is started at 2009-12-23:02/12/21
48469 2009-07-23 14:12:21,562 [QuartzScheduler_Worker-4] INFO DemoJob - 4916061 org.springtask.quartzjob.changable.simpletrigger.DemoJob is sleeping
58469 2009-07-23 14:12:31,562 [QuartzScheduler_Worker-4] INFO DemoJob - 4916061 sleep 10000
58469 2009-07-23 14:12:31,562 [QuartzScheduler_Worker-4] INFO DemoJob - 4916061 org.springtask.quartzjob.changable.simpletrigger.DemoJob is done
4916061 Job is started at 2009-12-23:02/12/36
63469 2009-07-23 14:12:36,562 [QuartzScheduler_Worker-5] INFO DemoJob - 4916061 org.springtask.quartzjob.changable.simpletrigger.DemoJob is sleeping
73469 2009-07-23 14:12:46,562 [QuartzScheduler_Worker-5] INFO DemoJob - 4916061 sleep 10000
73469 2009-07-23 14:12:46,562 [QuartzScheduler_Worker-5] INFO DemoJob - 4916061 org.springtask.quartzjob.changable.simpletrigger.DemoJob is done