Spring整理系列(08)——spring與quartz整合執行定時任務
專案基於maven進行管理。
一、例項專案程式碼示例:
1、pom.xml檔案所需要的基本jar:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId >com.test.spring</groupId>
<artifactId>support</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>support</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.2.6.RELEASE</spring.version >
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version >
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- quartz begin -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.1.3</version><!-- 1.8.5 2.1.3-->
</dependency>
<!-- quartz end -->
<!-- TEST begin -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!-- 預設的版本為3.8.1,修改為4.x,因為3.x使用的為程式設計的方式,4.x為註解的形式。 -->
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2、src/main/resources下spring-context.xml檔案,包含定時器排程配置:
<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="false">
<!-- 啟動自動掃描,自動裝配 -->
<context:component-scan base-package="com.test.spring.support"></context:component-scan>
<!-- 也可以作為單獨檔案匯入 -->
<!-- <import resource="classpath:support-quartz.xml"/> -->
<!--定義定時執行studentService 這個bean中的updateStudentStatus()方法 -->
<bean id="doJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!--你要執行的那個方法對應的bean -->
<property name="targetObject">
<ref bean="testQuartTimer" />
</property>
<!--你要執行那個方法,注意方法不能有返回值,引數好像也不能有 -->
<property name="targetMethod">
<value>testQuartTimerMethod</value>
</property>
</bean>
<!-- 排程觸發器 -->
<!--觸發器的bean的設定,在這裡我們設定了我們要觸發的jobDetail是哪個。
這裡我們定義了要觸發的jobDetail是searchEngerneTask,即觸發器去觸發哪個bean。
並且我們還定義了觸發的時間。
spring版本<3.1,quartz版本為1.x,class使用CronTriggerBean;
spring版本>3.1,quartz版本為2.x,class使用CronTriggerFactoryBean;儘量按這兩種方式使用-->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="doJob" />
</property>
<property name="cronExpression">
<!-- 執行週期的表示式;每小時:[0 0 * * * ?]; 每五鍾:[0 0/5 * * * ?]-->
<value>0/10 * * * * ?</value><!-- 10秒執行一次 -->
</property>
</bean>
<!-- 排程工廠 -->
<!--管理觸發器的總設定,管理我們的觸發器列表,可以在bean的list中放置多個觸發器。 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>
3、src/main/java下建立com.test.spring.support.TestQuartTimer類,作為定時器,執行主方法即可呼叫定時任務:
package com.test.spring.support;
import java.util.Scanner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
/**
* 測試定時器
*/
@Service
@Lazy(false)
public class TestQuartTimer {
/**
* @Date 建立時間:2016年8月9日 下午4:47:05
* @Description :定時任務執行
*/
public void testQuartTimerMethod(){
System.out.println("testQuartTimerMethod 定時任務執行中...");
}
/**
* @Description :主方法測試Quart定時任務
*/
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-context.xml");
//測試定時任務,阻塞主方法結束
Scanner input=new Scanner(System.in);
input.nextInt();
}
}
4、src/test/java下建立com.test.spring.support.TestSpringContextHolder類,通過單元測試執行定時器:
package com.test.spring.support;
import java.util.Scanner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 單元測試類
*/
public class TestSpringContextHolder{
@Test
public void testQuartzTime(){
//測試定時任務,阻塞單元測試方法結束
Scanner input=new Scanner(System.in);
int x=input.nextInt();
}
//以下為容器例項宣告及初始化、銷燬
private ClassPathXmlApplicationContext context;
@Before
public void before(){
try {
// xml檔案用逗號或者空格符隔開,均可載入
context = new ClassPathXmlApplicationContext("spring-context.xml");
context.start();
} catch (BeansException e) {
e.printStackTrace();
}
}
@After
public void after(){
context.destroy();
}
/**
* 從靜態變數applicationContext中取得Bean, 自動轉型為所賦值物件的型別.
*/
@SuppressWarnings("unchecked")
public <T> T getBean(String name) {
return (T) context.getBean(name);
}
/**
* 從靜態變數applicationContext中取得Bean, 自動轉型為所賦值物件的型別.
*/
public <T> T getBean(Class<T> requiredType) {
return context.getBean(requiredType);
}
}
5、如果web環境,需要servlet容器載入spring容器(同時注意新增spring web jar):
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
三、例項測試過程中發生的幾個異常
1、Spring與Quartz的版本問題,發生異常如下:
Caused by: java.lang.ClassNotFoundException: org.springframework.scheduling.quartz.CronTriggerBean
原因是Spring 3.0版本中內建的Quartz版本是<2.0的,在使用最新的Quartz包(>2.0)之後,出現版本不相容問題。
解決辦法有兩種:
1)、降低Quartz版本,降到1.X去,比如1.8.5,觸發器的bean使用**TriggerBean,比如org.springframework.scheduling.quartz.CronTriggerBean
。
2)、升級Spring版本到3.1+或4.x,將原來的**TriggerBean替換成**TriggerFactoryBean,例如org.springframework.scheduling.quartz.CronTriggerBean
就可以替換成 org.springframework.scheduling.quartz.CronTriggerFactoryBean
,替換之後問題解決。
2、異常如下:
Exception in thread "main" java.lang.NoClassDefFoundError: org/quartz/impl/JobDetailImpl
Caused by: java.lang.ClassNotFoundException: org.quartz.impl.JobDetailImpl
原因是quartz低版本中缺少JobDetailImpl這個類。
解決辦法是將quartz版本從1.x提高到2.x,測試2.1.3是沒有問題的。
3、異常如下:
java.lang.NoClassDefFoundError: org/springframework/transaction/TransactionException
Caused by: java.lang.ClassNotFoundException: org.springframework.transaction.TransactionException
原因是缺少spring的TransactionException類,即缺少spring框架相關jar包。
解決方法是新增org.springframework.spring-tx-3.1.1.RELEASE.jar,pom.xml檔案:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<!--<spring.version>4.2.6.RELEASE</spring.version>-->
<version>${spring.version}</version>
</dependency>