先看看quartz的持久化基本介绍:
引用
1 大家都清楚quartz最基本的概念就是job,在job内调用具体service完成具体功能,quartz需要把每个job存储起来,方便调度,quartz存储job方式就分三种,我们最常用的也是quartz默认的是RAMJobStore,RAMJobStore顾名思义就是把job的相关信息存储在内存里,如果用spring配置quartz的job信息的话,所有信息是配置在xml里,当spirng context启动的时候就把xml里的job信息装入内存。这一性质就决定了一旦JVM挂掉或者容器挂掉,内存中的job信息就随之消失,无法持久化。另外两种方式是JobStoreTX和JobStoreCMT,暂时不讨论这两者的区别,使用这两种JobStore,quartz就会通过jdbc直连或者应用服务器jndi连接数据库,读取配置在数据库里的job初始化信息,并且把job通过java序列化到数据库里,这样就使得每个job信息得到了持久化,即使在jvm或者容器挂掉的情况下,也能通过数据库感知到其他job的状态和信息。 2 quartz集群各节点之间是通过同一个数据库实例(准确的说是同一个数据库实例的同一套表)来感知彼此的。
由上可见,我们需要创建quartz要用的数据库表,此sql文件在:quartz-1.8.6docsdbTables。此文件夹下有各个数据库的sql文件,mysql选择tables_mysql.sql。创建相应表。
接下来新建quartz.properties来覆盖jar包中的此文件,新的properties文件放在src的根目录下即可。下面是文件内容:
- #==============================================================
- #Configure Main Scheduler Properties
- #==============================================================
- org.quartz.scheduler.instanceName = quartzScheduler
- org.quartz.scheduler.instanceId = AUTO
- #==============================================================
- #Configure JobStore
- #==============================================================
- org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
- org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
- org.quartz.jobStore.tablePrefix = QRTZ_
- org.quartz.jobStore.isClustered = true
- org.quartz.jobStore.clusterCheckinInterval = 20000
- org.quartz.jobStore.dataSource = myDS
- #==============================================================
- #Configure DataSource
- #==============================================================
- org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
- org.quartz.dataSource.myDS.URL = jdbc:mysql://192.168.20.195:3306/database?useUnicode=true&characterEncoding=UTF-8
- org.quartz.dataSource.myDS.user = root
- org.quartz.dataSource.myDS.password = 123456
- org.quartz.dataSource.myDS.maxConnections = 30
- #==============================================================
- #Configure ThreadPool
- #==============================================================
- org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
- org.quartz.threadPool.threadCount = 10
- org.quartz.threadPool.threadPriority = 5
- org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
#============================================================== #Configure Main Scheduler Properties #============================================================== org.quartz.scheduler.instanceName = quartzScheduler org.quartz.scheduler.instanceId = AUTO #============================================================== #Configure JobStore #============================================================== org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore.tablePrefix = QRTZ_ org.quartz.jobStore.isClustered = true org.quartz.jobStore.clusterCheckinInterval = 20000 org.quartz.jobStore.dataSource = myDS #============================================================== #Configure DataSource #============================================================== org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver org.quartz.dataSource.myDS.URL = jdbc:mysql://192.168.20.195:3306/database?useUnicode=true&characterEncoding=UTF-8 org.quartz.dataSource.myDS.user = root org.quartz.dataSource.myDS.password = 123456 org.quartz.dataSource.myDS.maxConnections = 30 #============================================================== #Configure ThreadPool #============================================================== org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 10 org.quartz.threadPool.threadPriority = 5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
可以看到除了数据源、线程池等配置外,我们指定了一个scheduler实例,实例ID为自动分配。
- #==============================================================
- #Configure Main Scheduler Properties
- #==============================================================
- org.quartz.scheduler.instanceName = quartzScheduler
- org.quartz.scheduler.instanceId = AUTO
#============================================================== #Configure Main Scheduler Properties #============================================================== org.quartz.scheduler.instanceName = quartzScheduler org.quartz.scheduler.instanceId = AUTO
此外,指定了集群相应配置,检查间隔为20s:
- org.quartz.jobStore.isClustered = true
- org.quartz.jobStore.clusterCheckinInterval = 20000
org.quartz.jobStore.isClustered = true org.quartz.jobStore.clusterCheckinInterval = 20000
最后配置applicant-context.xml文件。这里特别要注意一点:
引用
MethodInvokingJobDetailFactoryBean 类中的 methodInvoking 方法,是不支持序列化的,因此在把 QUARTZ 的 TASK 序列化进入数据库时就会抛错。
所以我们要自己实现MethodInvokingJobDetailFactoryBean 的功能,这里用MyDetailQuartzJobBean 替换。
- import java.lang.reflect.Method;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.scheduling.quartz.QuartzJobBean;
- public class MyDetailQuartzJobBean extends QuartzJobBean {
- protected final Log logger = LogFactory.getLog(getClass());
- private String targetObject;
- private String targetMethod;
- private ApplicationContext ctx;
- @Override
- protected void executeInternal(JobExecutionContext context)
- throws JobExecutionException {
- try {
- logger.info("execute [" + targetObject + "] at once>>>>>>");
- Object otargetObject = ctx.getBean(targetObject);
- Method m = null;
- try {
- m = otargetObject.getClass().getMethod(targetMethod, new Class[] {JobExecutionContext.class});
- m.invoke(otargetObject, new Object[] {context});
- } catch (SecurityException e) {
- logger.error(e);
- } catch (NoSuchMethodException e) {
- logger.error(e);
- }
- } catch (Exception e) {
- throw new JobExecutionException(e);
- }
- }
- public void setApplicationContext(ApplicationContext applicationContext) {
- this.ctx = applicationContext;
- }
- public void setTargetObject(String targetObject) {
- this.targetObject = targetObject;
- }
- public void setTargetMethod(String targetMethod) {
- this.targetMethod = targetMethod;
- }
import java.lang.reflect.Method; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.context.ApplicationContext; import org.springframework.scheduling.quartz.QuartzJobBean; public class MyDetailQuartzJobBean extends QuartzJobBean { protected final Log logger = LogFactory.getLog(getClass()); private String targetObject; private String targetMethod; private ApplicationContext ctx; @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { try { logger.info("execute [" + targetObject + "] at once>>>>>>"); Object otargetObject = ctx.getBean(targetObject); Method m = null; try { m = otargetObject.getClass().getMethod(targetMethod, new Class[] {JobExecutionContext.class}); m.invoke(otargetObject, new Object[] {context}); } catch (SecurityException e) { logger.error(e); } catch (NoSuchMethodException e) { logger.error(e); } } catch (Exception e) { throw new JobExecutionException(e); } } public void setApplicationContext(ApplicationContext applicationContext) { this.ctx = applicationContext; } public void setTargetObject(String targetObject) { this.targetObject = targetObject; } public void setTargetMethod(String targetMethod) { this.targetMethod = targetMethod; }
终于到配置spring文件这步了
- <bean id="mapScheduler" lazy-init="false" autowire="no"
- class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
- <list>
- <ref bean="dailyTrigger" />
- <ref bean="billCountTrigger" />
- <ref bean="userAcctTrigger" />
- </list>
- </property>
- <property name="applicationContextSchedulerContextKey" value="applicationContext" />
- <property name="configLocation" value="classpath:quartz.properties" />
- </bean>
- <bean id="dailyBillJob" class="com.***.job.DailyBillJob" />
- <bean id="dailyBillJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
- <property name="jobClass">
- <value>com.autelan.auteview.lib.util.MyDetailQuartzJobBean
- </value>
- </property>
- <property name="jobDataAsMap">
- <map>
- <entry key="targetObject" value="dailyBillJob" />
- <entry key="targetMethod" value="execute" />
- </map>
- </property>
- </bean>
- <bean id="dailyTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail">
- <ref bean="dailyBillJobDetail" />
- </property>
- <property name="cronExpression">
- <value>11 11 11 * * ?</value>
- </property>
- </bean>
- // 转载请注明出处http://forhope.iteye.com/blog/1398990
<bean id="mapScheduler" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="dailyTrigger" /> <ref bean="billCountTrigger" /> <ref bean="userAcctTrigger" /> </list> </property> <property name="applicationContextSchedulerContextKey" value="applicationContext" /> <property name="configLocation" value="classpath:quartz.properties" /> </bean> <bean id="dailyBillJob" class="com.***.job.DailyBillJob" /> <bean id="dailyBillJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass"> <value>com.autelan.auteview.lib.util.MyDetailQuartzJobBean </value> </property> <property name="jobDataAsMap"> <map> <entry key="targetObject" value="dailyBillJob" /> <entry key="targetMethod" value="execute" /> </map> </property> </bean> <bean id="dailyTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="dailyBillJobDetail" /> </property> <property name="cronExpression"> <value>11 11 11 * * ?</value> </property> </bean>