Cluster环境下,配置Quartz job为集群模式后,配置示例如下,其中红色部分与集群模式相关。我使用的是quartz-all-1.6.0,spring-context-support-3.0.1。
1 <bean id="startQuertz" lazy-init="false" autowire="no"
2 class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
3 <property name="triggers">
4 <list>
5 <ref bean="jobTrigger" />
6 </list>
7 </property>
8 <property name="dataSource" ref="dataSource" />
9 <property name="transactionManager" ref="transactionManager" />
10 <property name="quartzProperties">
11 <props>
12 <prop key="org.quartz.scheduler.instanceName">DataScheduler</prop>
13 <prop key="org.quartz.scheduler.instanceId">AUTO</prop>
14 <prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
15 <prop key="org.quartz.threadPool.threadCount">5</prop>
16 <prop key="org.quartz.threadPool.threadPriority">5</prop>
17 <prop key="org.quartz.jobStore.misfireThreshold">60000</prop>
18 <prop key="org.quartz.jobStore.class">org.springframework.scheduling.quartz.LocalDataSourceJobStore</prop>
19 <prop key="org.quartz.jobStore.tablePrefix">QRTZ_</prop>
20 <prop key="org.quartz.jobStore.isClustered">true</prop>
21 <prop key="org.quartz.jobStore.useProperties">false </prop>
22 </props>
23 </property>
24 </bean>
但系统运行Quartz job抛出如下的错误信息:
1 Caused by: org.quartz.JobPersistenceException: Couldn't store job: Unable to serialize JobDataMap for insertion into database because the value of property 'methodInvoker' is not serializable: org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean [See nested exception: java.io.NotSerializableException: Unable to serialize JobDataMap for insertion into database because the value of property 'methodInvoker' is not serializable: org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean]
2 at org.quartz.impl.jdbcjobstore.JobStoreSupport.storeJob(JobStoreSupport.java:1041)
3 at org.quartz.impl.jdbcjobstore.JobStoreSupport$4.execute(JobStoreSupport.java:1011)
4 at org.quartz.impl.jdbcjobstore.JobStoreSupport$39.execute(JobStoreSupport.java:3590)
5 at org.quartz.impl.jdbcjobstore.JobStoreCMT.executeInLock(JobStoreCMT.java:244)
6 at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInLock(JobStoreSupport.java:3586)
7 at org.quartz.impl.jdbcjobstore.JobStoreSupport.storeJob(JobStoreSupport.java:1007)
8 at org.quartz.core.QuartzScheduler.addJob(QuartzScheduler.java:785)
9 at org.quartz.impl.StdScheduler.addJob(StdScheduler.java:278)
10 at org.springframework.scheduling.quartz.SchedulerAccessor.addJobToScheduler(SchedulerAccessor.java:311)
11 at org.springframework.scheduling.quartz.SchedulerAccessor.addTriggerToScheduler(SchedulerAccessor.java:334)
12 at org.springframework.scheduling.quartz.SchedulerAccessor.registerJobsAndTriggers(SchedulerAccessor.java:271)
13 at org.springframework.scheduling.quartz.SchedulerFactoryBean.afterPropertiesSet(SchedulerFactoryBean.java:513)
网上google一番,发现这个是Spring的一个Bug,详情参见:http://forum.springsource.org/showthread.php?31820-Quartz-and-JDBCJobStore,按照论坛中的方案修改Spring包中的2个java文件后解决。
其他的注意事项
1. Quartz job类要实现java.io.Serializable接口,不然还是会报NotSerializableException的异常。
2. Quartz job类的属性也要实现java.io.Serializable接口。
参考