Spring3.0不支持Quartz2.0,因为org.quartz.CronTrigger在2.0从class变成了一个interface造成IncompatibleClassChangeError错误:
Caused by: java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class
今天刚下载了Spring3.2.4,发现这个新版本已经支持quartz2.x了.以前的Spring版本只支持Quartz-1.8.x及以前的版本,做个小实例分享一下.
注:Spring3.2.4配置文件中使用CronTriggerFactoryBean来集成quartz2.x,使用CronTriggerBean来集成quartz1.8.x及以前版本.
1. main方法入口类: MainTest.java,内容如下:
package com.sse.quartz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* Created on 2013-12-12
* <p>Title: XXXX系统_[模块名]/p>
* <p>Description: [描述该类概要功能介绍]</p>
* <p>Copyright: Copyright (c) 2011</p>
* <p>Company: </p>
* <p>Department: 网站运维部</p>
* @author zhl
*/
public class MainTest {
public static void main(String[] args) {
System.out.println(" Test start . ");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化
// context.getBean("startQuertz");
System.out.print(" Test end .. ");
}
}
2. applicationContext.xml配置文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Spring配置文件的Schema信息 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 启动触发器的配置开始
<bean name="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myJobTrigger" />
</list>
</property>
</bean> -->
<!-- 启动触发器的配置结束 -->
<!-- 调度的配置开始 -->
<!--
quartz-1.8以前的配置
<bean id="myJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="myJobDetail" />
</property>
<property name="cronExpression">
<value>0/1 * * * * ?</value>
</property>
</bean>
-->
<!-- quartz-2.x的配置
<bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="myJobDetail" />
</property>
<property name="cronExpression">
<value>0/1 * * * * ?</value>
</property>
</bean> -->
<!-- 调度的配置结束 -->
<!-- job的配置开始
<bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="myJob" />
</property>
<property name="targetMethod">
<value>work</value>
</property>
</bean> -->
<!-- job的配置结束 -->
<!-- 工作的bean
<bean id="myJob" class="com.sse.quartz.MyJob" /> -->
<!-- 上边为bean配置,下边为配置文件配置,两个不可都放开(放开上边则调用work文法,放下下边则调用execute方法)
如果用上边配置则不需要实现job接口-->
<bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="configLocation" value="classpath:quartz.properties" />
</bean>
</beans>
3. quartz.properties 配置文件:
#============================================================================
# Quartz 调度任务所需的配置文件
#============================================================================
#org.quartz.scheduler.instanceName属性可为任何值,用在 JDBC JobStore 中来唯一标识实例,但是所有集群节点中必须相同。
org.quartz.scheduler.instanceName = QuartzScheduler
#org.quartz.scheduler.instanceId 属性为 AUTO即可,基于主机名和时间戳来产生实例 ID。
org.quartz.scheduler.instanceId = AUTO
#==================================