1. 程式人生 > >使用spring+quartz配置多個定時任務

使用spring+quartz配置多個定時任務

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 is java.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"?>
<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>
web.xml, 在spring容器啟動時讀取載入applicationContext.xml配置檔案
<!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等容器中, 啟動後, 就可以看到定時器執行效果, 如下圖所示