Spring4 + Quartz-2.2.0集成实例

时间:2023-03-08 15:57:23

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及以前版本.

代码结构及引用jar见下图:

Spring4 + Quartz-2.2.0集成实例

applicationContext.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <!-- 启动触发器的配置开始 -->
  7. <bean name="startQuertz" lazy-init="false" autowire="no"
  8. class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  9. <property name="triggers">
  10. <list>
  11. <ref bean="myJobTrigger" />
  12. </list>
  13. </property>
  14. </bean>
  15. <!-- 启动触发器的配置结束 -->
  16. <!-- 调度的配置开始 -->
  17. <!--
  18. quartz-1.8以前的配置
  19. <bean id="myJobTrigger"
  20. class="org.springframework.scheduling.quartz.CronTriggerBean">
  21. <property name="jobDetail">
  22. <ref bean="myJobDetail" />
  23. </property>
  24. <property name="cronExpression">
  25. <value>0/1 * * * * ?</value>
  26. </property>
  27. </bean>
  28. -->
  29. <!-- quartz-2.x的配置 -->
  30. <bean id="myJobTrigger"
  31. class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
  32. <property name="jobDetail">
  33. <ref bean="myJobDetail" />
  34. </property>
  35. <property name="cronExpression">
  36. <value>0/1 * * * * ?</value>
  37. </property>
  38. </bean>
  39. <!-- 调度的配置结束 -->
  40. <!-- job的配置开始 -->
  41. <bean id="myJobDetail"
  42. class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  43. <property name="targetObject">
  44. <ref bean="myJob" />
  45. </property>
  46. <property name="targetMethod">
  47. <value>work</value>
  48. </property>
  49. </bean>
  50. <!-- job的配置结束 -->
  51. <!-- 工作的bean -->
  52. <bean id="myJob" class="com.tgb.lk.demo.quartz.MyJob" />
  53. </beans>

MyJob.java:

  1. import java.util.Date;
  2. public class MyJob {
  3. public void work() {
  4. System.out.println("date:" + new Date().toString());
  5. }
  6. }

web.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6. <!-- Spring config start -->
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  10. </context-param>
  11. <listener>
  12. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  13. </listener>
  14. <!-- Spring config end -->
  15. <welcome-file-list>
  16. <welcome-file>index.jsp</welcome-file>
  17. </welcome-file-list>
  18. </web-app>

代码下载地址:http://download.csdn.net/detail/lk_blog/6277021

log4j.logger.org.quartz=INFO   控制输出日志