使用Spring + quartz集群持久化时注意事项

1、持久化时未序列化异常

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

例如:

[html]  view plain  copy
  1. <bean id="studyDetail"    
  2.     class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">    
  3.     <property name="targetObject">    
  4.         <ref bean="studyJob" />    
  5.     </property>    
  6.     <property name="targetMethod">    
  7.         <value>doSth</value>    
  8.     </property>    
  9. </bean>   


原因请看org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean

[html]  view plain  copy
  1. NOTE: JobDetails created via this FactoryBean are not serializable and thus not suitable for persistent job stores. You need to implement your own Quartz Job as a thin wrapper for each case where you want a persistent job to delegate to a specific service method.  
  2.   
  3. Compatible with Quartz 1.5+ as well as Quartz 2.0-2.2, as of Spring 3.2.  

所以对于持久化的job需要自己继承org.springframework.scheduling.quartz.QuartzJobBean,同时修改JobDetailFactoryBean为

[html]  view plain  copy
  1. <bean id="studyDetail"  
  2.     class="org.springframework.scheduling.quartz.JobDetailFactoryBean">  
  3.     <property name="jobClass">  
  4.         <value>com.joshua.job.StudyJob</value>  
  5.     </property>  
  6.     <property name="name" value="studyDetail"></property>  
  7.     <property name="durability" value="true" />  
  8. </bean>   

2.持久化时jobDetail找不到

[java]  view plain  copy
  1. org.quartz.JobPersistenceException: The job (DEFAULT.studyDetail) referenced by the trigger does not exist.  

原因一般是数据源配置导致的问题。

可能的原因是:数据源未配置成自动提交,当第一次启动trigger时,之前对数据库的job的增加的事物没有自动提交,导致后面的事物无法查询到。

如果数据源是通过dbcp配置的将自动提交配置为true

[html]  view plain  copy
  1. <property name="defaultAutoCommit" value="false" />  
如果是c3p0配置为autoCommitOnClose=true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值