Quartz是實現了序列化接口的,包括接口,所以可以使用標准方式序列化到數據庫。
而Spring2.5.6在集成Quartz時卻未能考慮持久化問題。
Spring對JobDetail進行了封裝,卻未實現序列化接口,所以持久化的時候會產生NotSerializable問題,這也是網上一直在那邊叫囂為什么不能持久化到數據庫問題,哥今天看了下Spring源碼,發現Spring對Quartz持久化的問題.
1. 不知道Spring未來會不會對持久化的支持,不過我們可以有如下解決方案,比如改寫
Spring的代碼,實現序列化接口.
2. 不使用Spring的Fatory,自己實現任務的初始化.
既然Spring不支持持久化,那么持久化任務還是自己編寫實現吧,否則每次都需要打包發布,麻煩,自己編寫的類與Quartz完全兼容.
注意:為什么Spring不支持外配置任務,可能也是考慮到這方面問題所以才不提供這些任務的執行化支持.[配置文件配置與數據庫配置重復]
直接使用Quartz是支持序列化功能,比如直接使用頁面配置Quartz界面,設置任務執行時間等屬性。
通過配置實現的是不應該初始化到數據庫,否則直接在數據庫中配置了。不過也是可以配置的,通過改寫JobDetailBean.代碼如下:
- package org.frame.auth.service;
- import java.util.Map;
- import org.quartz.Job;
- import org.quartz.JobDetail;
- import org.quartz.Scheduler;
- import org.springframework.beans.factory.BeanNameAware;
- import org.springframework.beans.factory.InitializingBean;
- import org.springframework.scheduling.quartz.DelegatingJob;
- import org.springframework.scheduling.quartz.SchedulerFactoryBean;
- public class PersistentJobDetailBean extends JobDetail
- implements BeanNameAware, InitializingBean {
- private static final long serialVersionUID = -4389885435844732405L;
- private Class actualJobClass;
- private String beanName;
- /**
- * Overridden to support any job class, to allow a custom JobFactory
- * to adapt the given job class to the Quartz Job interface.
- * @see SchedulerFactoryBean#setJobFactory
- */
- public void setJobClass(Class jobClass) {
- if (jobClass != null && !Job.class.isAssignableFrom(jobClass)) {
- super.setJobClass(DelegatingJob.class);
- this.actualJobClass = jobClass;
- }
- else {
- super.setJobClass(jobClass);
- }
- }
- /**
- * Overridden to support any job class, to allow a custom JobFactory
- * to adapt the given job class to the Quartz Job interface.
- */
- public Class getJobClass() {
- return (this.actualJobClass != null ? this.actualJobClass : super.getJobClass());
- }
- /**
- * Register objects in the JobDataMap via a given Map.
- * <p>These objects will be available to this Job only,
- * in contrast to objects in the SchedulerContext.
- * <p>Note: When using persistent Jobs whose JobDetail will be kept in the
- * database, do not put Spring-managed beans or an ApplicationContext
- * reference into the JobDataMap but rather into the SchedulerContext.
- * @param jobDataAsMap Map with String keys and any objects as values
- * (for example Spring-managed beans)
- * @see SchedulerFactoryBean#setSchedulerContextAsMap
- */
- public void setJobDataAsMap(Map jobDataAsMap) {
- getJobDataMap().putAll(jobDataAsMap);
- }
- /**
- * Set a list of JobListener names for this job, referring to
- * non-global JobListeners registered with the Scheduler.
- * <p>A JobListener name always refers to the name returned
- * by the JobListener implementation.
- * @see SchedulerFactoryBean#setJobListeners
- * @see org.quartz.JobListener#getName
- */
- public void setJobListenerNames(String[] names) {
- for (int i = 0; i < names.length; i++) {
- addJobListener(names[i]);
- }
- }
- public void setBeanName(String beanName) {
- this.beanName = beanName;
- }
- public void afterPropertiesSet() {
- if (getName() == null) {
- setName(this.beanName);
- }
- if (getGroup() == null) {
- setGroup(Scheduler.DEFAULT_GROUP);
- }
- }
- }
這里把Spring的ApplicationContext去掉了,因為這個屬性沒有實現序列化接口。其他配置與原告一致:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd ">
- <beans default-autowire="byName">
- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="url" >
- <value><![CDATA[jdbc:mysql://localhost:3306/txl?connectTimeout=1000&useUnicode=true&characterEncoding=utf-8]]></value>
- </property>
- <property name="username" value="root"/>
- <property name="password" value=""/>
- </bean>
- <bean id="jobDetail" class = "org.frame.auth.service.PersistentJobDetailBean">
- <property name="jobClass" value="org.frame.auth.service.PersistentJob"></property>
- </bean>
- <!-- <bean id="trigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean" >-->
- <!-- <property name="jobDetail" ref="jobDetail"></property>-->
- <!-- <property name="startDelay" value="1000"></property>-->
- <!-- <property name="repeatInterval" value="3000"></property>-->
- <!-- <property name="jobDataAsMap">-->
- <!-- <map>-->
- <!-- <entry key="message" value="this is trigger"></entry>-->
- <!-- </map>-->
- <!-- </property>-->
- <!-- </bean>-->
- <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean" >
- <property name="jobDetail" ref="jobDetail"/>
- <property name="cronExpression">
- <value>0/10 * * * * ?</value>
- </property>
- </bean>
- <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="dataSource" ref="dataSource"></property>
- <property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
- <property name="configLocation" value="classpath:quartz.properties"/>
- </bean>
- </beans>
org.frame.auth.service.PersistentJob這個類很簡單,如下:
- package org.frame.auth.service;
- import org.quartz.Job;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- public class PersistentJob implements Job {
- @Override
- public void execute(JobExecutionContext context) throws JobExecutionException {
- System.out.println("spring quartz!");
- }
- }
有人可能會說,你這種任務調度持久化就沒有意義了,是的,一般持久化到數據庫的代碼如下:
- package org.frame.auth.service;
- import java.util.Map;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.quartz.StatefulJob;
- public class PersistentJob implements StatefulJob {
- @Override
- public void execute(JobExecutionContext context) throws JobExecutionException {
- // TODO Auto-generated method stub
- Map map = context.getJobDetail().getJobDataMap();
- System.out.println("["+context.getJobDetail().getName()+"]"+map.get("message"));
- map.put("message", "updated Message");
- }
- }
這樣的話,信息message就會持久化到數據庫中了.可以建立系統的連鎖調度,這根據你的業務需求了.
在Spring中配置的任務通過我這種修改是可以運行,不過每次運行都需要把原先的任務刪除,否則會提示任務已經存在,Quartz的優勢是就算服務器停止,下次重啟能夠恢復原先的任務並繼續執行.