Spring被用在了越来越多的项目中, quartz也被公认为是比较好用的定时器设置工具, 在这里通过一个demo说明如何使用spring和quartz配置多个定时任务.
环境: eclipse + maven + spring 3.0.6.RELEASE版本 + quartz 1.8.6版本
一. 准备工作
项目目录结构截图:
需要引入的jar包, 这里是采用maven的dependency依赖; 如果没有使用maven, 可以对照着上面的jar包截图一一进行下载. 这里唯一需要注意的就是, 如果使用spring 3.1以下的版本, quartz就需要相应的2.0以下版本, 否则会导致 org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.scheduling.quartz.CronTriggerBean] for bean with name 'cronTrigger_1' defined in file [E:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\git-common\WEB-INF\classes\applicationContext.xml]: problem with class file or dependent class; nested exception isjava.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class 错误.
后续: 后来使用了spring 4.0.5.RELEASE版本+quartz 2.2.1版本, 还是报出了以上错误, 将quartz降低为1.8.6版本后, 问题消失...
<properties>二. 程序代码及配置代码
<springframework.version>3.0.6.RELEASE</springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.6</version>
</dependency>
</dependencies>
测试代码就简单的打印出一句话
package com.quartz.demo;
import java.util.Date;
public class Job1Demo {
public void sayHello() {
System.out.println(new Date() + " -> Hello, 我是任务 1");
}
}
package com.quartz.demo;import java.util.Date;public class Job2Demo { public void sayHello() { System.out.println(new Date() + " -> Hello, 我是任务 2"); }}Spring配置文件applicationContext.xml中配置了要注入的bean, 定时任务, 任务执行周期时间等
<?xml version="1.0" encoding="UTF-8"?>web.xml, 在spring容器启动时读取加载applicationContext.xml配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 任务1配置 -->
<bean name="job1" class="com.quartz.demo.Job1Demo"/>
<bean id="jobDetail_1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 执行的类 -->
<property name="targetObject">
<ref bean="job1" />
</property>
<!-- 类中的方法 -->
<property name="targetMethod">
<value>sayHello</value>
</property>
</bean>
<bean id="cronTrigger_1" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobDetail_1" />
</property>
<!-- 每一秒钟执行一次 -->
<property name="cronExpression">
<value>0/1 * * * * ?</value>
</property>
</bean>
<!-- 任务2配置 -->
<bean name="job2" class="com.quartz.demo.Job2Demo"/>
<bean id="jobDetail_2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="job2" />
</property>
<property name="targetMethod">
<value>sayHello</value>
</property>
</bean>
<bean id="cronTrigger_2" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobDetail_2" />
</property>
<property name="cronExpression">
<value>0/1 * * * * ?</value>
</property>
</bean>
<!-- 总配置 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 添加触发器 -->
<property name="triggers">
<list>
<ref bean="cronTrigger_1" />
<ref bean="cronTrigger_2" />
</list>
</property>
</bean>
</beans>
<!DOCTYPE web-app PUBLIC三. 执行结果
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:*.xml
</param-value>
</context-param>
<!-- Spring 容器启动监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
将web项目部署到tomcat等容器中, 启动后, 就可以看到定时器执行效果, 如下图所示