特别注意一点,与Spring3.1以下版本整合必须使用Quartz1,最初我拿2.2.1的,怎么搞都报错:
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.scheduling.quartz.CronTriggerBean] for bean with name 'mytrigger' defined in class path resource [applicationContext.xml]: problem with class file or dependent class; nested exception is java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class查看发现spring3.0.5中org.springframework.scheduling.quartz.CronTriggerBean继承了org.quartz.CronTrigger(public class CronTriggerBeanextends CronTrigger),而在quartz2.1.3中org.quartz.CronTrigger是个接口(publicabstract interface CronTrigger extends Trigger),而在quartz1.8.5及1.8.4中org.quartz.CronTrigger是个类(publicclass CronTrigger extends Trigger),从而造成无法在applicationContext中配置触发器。这是spring3.1以下版本和quartz2版本不兼容的一个bug。(感谢tiren的回复,spring3.1以及以后版本支持quartz2)
所以,我们在博文《Spring之——quartz的配置方式(集群与不集群) 》《Spring之——quartz集群(MySQL数据源)》《Spring之——quartz集群(Oracle数据源)》中,均使用的是1.x版本的quartz,那么,2.x版本的quartz如何配置呢。
那么,下面我们就基于Spring4.0+quartz2.2.1的配置来向大家介绍如何实现这样的架构。
1、创建quartz.properties文件
这个文件名称不可更改,将文件路径放置在src目录下,内容如下:
- #==============================================================
- #Configure Main Scheduler Properties
- #==============================================================
- org.quartz.scheduler.instanceName = mapScheduler
- 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
- org.quartz.jobStore.maxMisfiresToHandleAtATime = 1
- org.quartz.jobStore.misfireThreshold = 120000
- org.quartz.jobStore.txIsolationLevelSerializable = true
- #==============================================================
- #Configure DataSource
- #==============================================================
- org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
- org.quartz.dataSource.myDS.URL = jdbc:mysql://192.168.254.120:3306/test?useUnicode=true&characterEncoding=UTF-8
- org.quartz.dataSource.myDS.user = root
- org.quartz.dataSource.myDS.password = root
- org.quartz.dataSource.myDS.maxConnections = 30
- org.quartz.jobStore.selectWithLockSQL = SELECT * FROM {0}LOCKS WHERE LOCK_NAME = ? FOR UPDATE
- #==============================================================
- #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
- #==============================================================
- #Skip Check Update
- #update:true
- #not update:false
- #==============================================================
- org.quartz.scheduler.skipUpdateCheck = true
- #============================================================================
- # Configure Plugins
- #============================================================================
- org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
- org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
- org.quartz.plugin.shutdownhook.cleanShutdown = true
2.重写 quartz 的 QuartzJobBean 类
原因是在使用 quartz+spring 把 quartz 的 task 实例化进入数据库时,会产生: serializable 的错误,原因在于:- <bean id="jobtask" class="org.springframework.scheduling.quartz. MethodInvokingJobDetailFactoryBean ">
- <property name="targetObject">
- <ref bean="quartzJob"/>
- </property>
- <property name="targetMethod">
- <value>execute</value>
- </property>
- </bean>
必须根据 QuartzJobBean 来重写一个自己的类,然后使用 SPRING 把这个重写的类(我们就名命它为: MyDetailQuartzJobBean )注入 appContext 中后,再使用 AOP 技术反射出原有的 quartzJobx( 就是开发人员原来已经做好的用于执行 QUARTZ 的 JOB 的执行类 ) 。 我们这个主要是通过反射机制调用实际要执行的操作方法,所以,调用的时候要符合反射机制的规范。
- package com.cdsmartlink.framework.quartz.job;
- import java.lang.reflect.Method;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.scheduling.quartz.QuartzJobBean;
- /**
- * Spring调度任务
- * @author liuyazhuang
- *
- */
- public class MyDetailQuartzJobBean extends QuartzJobBean{
- private String targetObject;
- private String targetMethod;
- private ApplicationContext ctx;
- @Override
- protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
- try {
- //LogUtils.Log("execute [" + targetObject + "] at once>>>>>>");
- Object otargetObject = ctx.getBean(targetObject);
- Method m = null;
- try {
- m = otargetObject.getClass().getMethod(targetMethod, new Class[] {JobExecutionContext.class}); //方法中的参数是JobExecutionContext类型
- m.invoke(otargetObject, new Object[] {context});
- } catch (SecurityException e) {
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- }
- } 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;
- }
- }
3、创建在quartz中执行的类和方法
其他service中一些具体业务类和方法省略,这里只介绍如何配置quartz和如何使之正确工作
- package com.cdsmartlink.analysis.proxy;
- import javax.annotation.Resource;
- import org.quartz.JobExecutionContext;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Component;
- import com.cdsmartlink.analysis.service.AnalysisLogService;
- import com.cdsmartlink.common.log.service.VisitLogService;
- import com.cdsmartlink.utils.log.LogUtils;
- /**
- * 调度分析日志
- * @author liuyazhuang
- *
- */
- @Component
- public class AnalysisScheduleTask {
- static final Logger logger= LoggerFactory.getLogger(AnalysisScheduleTask.class);
- @Resource
- private AnalysisLogService analysisLogService;
- @Resource
- private VisitLogService visitLogService;
- /**
- * 调度创建表,方法中的参数是JobExecutionContext类型,要使MyDetailQuartzJobBean中的executeInternal方法中利用反射机制调用到相应的方法
- */
- public void createTableTask(JobExecutionContext context){
- LogUtils.Log("AnalysisScheduleTask=====动态创建表开始");
- for(int i = 0; i < 3; i++){
- visitLogService.createTables(i);
- }
- LogUtils.Log("AnalysisScheduleTask=====动态创建表结束");
- }
- /**
- * 调度任务栈,方法中的参数是JobExecutionContext类型,要使MyDetailQuartzJobBean中的executeInternal方法中利用反射机制调用到相应的方法
- */
- public void secheduleTask(JobExecutionContext context){
- logger.debug("secheduleTask");
- analysisLogService.analysis();
- }
- /**
- * 删除任务栈,方法中的参数是JobExecutionContext类型,要使MyDetailQuartzJobBean中的executeInternal方法中利用反射机制调用到相应的方法
- */
- public void deleteTask(JobExecutionContext context){
- logger.debug("deleteTask");
- visitLogService.delete();
- }
- }
4、配置spring-quartz.xml文件
文件内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <!-- 注册调度任务 -->
- <bean id="mapScheduler" lazy-init="false" autowire="no"
- class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy">
- <!--可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了 -->
- <property name="overwriteExistingJobs" value="true" />
- <!--必须的,QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动 -->
- <property name="startupDelay" value="30" />
- <!-- 设置自动启动 -->
- <property name="autoStartup" value="true" />
- <property name="triggers">
- <list>
- <ref bean="createTableShelvesTrigger" />
- </list>
- </property>
- <property name="applicationContextSchedulerContextKey" value="applicationContext" />
- <property name="configLocation" value="classpath:quartz.properties" />
- </bean>
- <!-- 配置处理日志任务 -->
- <bean name="analysisScheduleTask" class="com.cdsmartlink.analysis.proxy.AnalysisScheduleTask"></bean>
- <!-- 动态创建日志记录表 -->
- <bean id="createTableShelvesInvokingJob"
- class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
- <!-- durability 表示任务完成之后是否依然保留到数据库,默认false -->
- <property name="durability" value="true" />
- <property name="requestsRecovery" value="true" />
- <property name="jobClass">
- <value>com.cdsmartlink.framework.quartz.job.MyDetailQuartzJobBean</value>
- </property>
- <property name="jobDataAsMap">
- <map>
- <entry key="targetObject" value="analysisScheduleTask" />
- <entry key="targetMethod" value="createTableTask" />
- </map>
- </property>
- </bean>
- <!-- 定期动态创建数据表 -->
- <bean id="createTableShelvesTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
- <property name="jobDetail" ref="createTableShelvesInvokingJob"/>
- <!-- 每月最后一天的23:55分执行任务 -->
- <property name="cronExpression" value="0 55 23 L * ?"/>
- </bean>
- <!-- 注册调度任务 -->
- <!-- <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false" destroy-method="destroy">
- <property name="triggers">
- <list>
- <ref bean="createTableShelvesTrigger"/>
- </list>
- </property>
- </bean> -->
- </beans>
与上一篇博文《Spring之——quartz的配置方式(集群与不集群) 》的架构相比,最重要的不同是spring-quartz.xml文件的配置不同。
5、将spring-quartz.xml文件导入到Spring的配置文件applicationContext.xml文件中。
6、创建存储quartz相关数据表
下载quartz-2.2.1包,解压,在quartz-2.2.1-distribution/quartz-2.2.1/docs/dbTables中有各个数据库的SQL运行脚本文件,我们只需要打开相应的数据库运行相应的脚本文件就可以在数据库中创建存储quartz相关的数据表了。
至此,大功告成。
附录. 可用于设置 JobStoreTX 的配置属性
属性 | 默认值 |
org.quartz.jobStore.driverDelegateClass | |
描述:能理解不同数据库系统中某一特定方言的驱动代理 | |
org.quartz.jobStore.dataSource | |
描述:用于 quartz.properties 中数据源的名称 | |
org.quartz.jobStore.tablePrefix | QRTZ_ |
描述:指定用于 Scheduler 的一套数据库表名的前缀。假如有不同的前缀,Scheduler 就能在同一数据库中使用不同的表。 | |
org.quartz.jobStore.userProperties | False |
描述:"use properties" 标记指示着持久性 JobStore 所有在JobDataMap 中的值都是字符串,因此能以 名-值 对的形式存储,而不用让更复杂的对象以序列化的形式存入 BLOB 列中。这样会更方便,因为让你避免了发生于序列化你的非字符串的类到 BLOB 时的有关类版本的问题。 | |
org.quartz.jobStore.misfireThreshold | 60000 |
描述:在 Trigger 被认为是错过触发之前,Scheduler 还容许 Trigger 通过它的下次触发时间的毫秒数,默认值(假如你未在配置中存在这一属性条目) 是60000(60 秒)。这个不仅限于JDBC-JobStore;它也可作为RAMJobStore 的参数 | |
org.quartz.jobStore.isClustered | False |
描述:设置为 true 打开集群特性。如果你有多个 Quartz 实例在用同一套数据库时,这个属性就必须设置为 true。 | |
org.quartz.jobStore.clusterCheckinInterval | 15000 |
描述:设置一个频度(毫秒),用于实例报告给集群中的其他实例。这会影响到侦测失败实例的敏捷度。它只用于设置了 isClustered 为 true 的时候。 | |
org.quartz.jobStore.maxMisfiresToHandleAtATime | 20 |
描述:这是 JobStore 能处理的错过触发的 Trigger 的最大数量。处理太多(超过两打) 很快会导致数据库表被锁定够长的时间,这样就妨碍了触发别的(还未错过触发) trigger 执行的性能。 | |
org.quartz.jobStore.dontSetAutoCommitFalse | False |
描述:设置这个参数为 true 会告诉 Quartz 从数据源获取的连接后不要调用它的 setAutoCommit(false) 方法。这在少些情况下是有帮助的,比如假如你有这样一个驱动,它会抱怨本来就是关闭的又来调用这个方法。这个属性默认值是 false,因为大多数的驱动都要求调用setAutoCommit(false)。 | |
org.quartz.jobStore.selectWithLockSQL | SELECT * FROM {0}LOCKS WHERE LOCK_NAME = ? FOR UPDATE |
描述:这必须是一个从 LOCKS 表查询一行并对这行记录加锁的 SQL 语句。假如未设置,默认值就是 SELECT * FROM {0}LOCKS WHERE LOCK_NAME = ? FOR UPDATE,这能在大部分数据库上工作。{0} 会在运行期间被前面你配置的TABLE_PREFIX 所替换。 | |
org.quartz.jobStore.txIsolationLevelSerializable | False |
描述:值为 true 时告知 Quartz(当使用 JobStoreTX 或 CMT) 调用 JDBC 连接的setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE) 方法。这有助于阻止某些数据库在高负载和长时间事物时锁的超时。 |