特别注意一点,与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>