1. 程式人生 > >spring quartz定時任務整合配置(ssm,ssh專案都適用)

spring quartz定時任務整合配置(ssm,ssh專案都適用)

 

轉自:https://www.tpyyes.com/a/javaweb/2017/0310/79.html

spring整合配置quartz定時器任務非常的簡單,本專案是基於ssm maven專案的整合,如果你是ssh專案,請下載spring-context-support-4.1.0.RELEASE.jar和quartz-2.2.1.rar相關jar包,匯入到lib目錄中。

先認識一下quartz定時任務分為簡單觸發器和任務觸發器,前者可以做一些簡單的任務處理,後者任務觸發器可以做非常複雜的定時任務,比如說2017年3月20日22點每隔5分鐘清理一下mysql資料庫裡面的垃圾資料。

quartz任務觸發器需要用到quartz表示式,quartz表示式用於設定任務在什麼時候執行,如果不瞭解可以看quartz表示式簡單講解:

http://www.tpyyes.com/a/javaweb/2017/0308/78.html

quartz簡單觸發器主要用於處理每隔多少分鐘,多少天等這樣簡單的任務,和java中的Timer類的功能一樣,如下:http://www.tpyyes.com/a/java/2017/0307/77.html

下面開始spring quartz定時器整合。

1.maven pom.xml匯入spring quartz整合jar包,如下。

<!-- quartz定時器jar包 -->
<dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz</artifactId>
	<version>2.2.1</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context-support</artifactId>
	<version>4.1.4.RELEASE</version>
</dependency>

2.建立spring-quartz.xml檔案,並在web.xml中能夠掃描得到spring-quartz.xml檔案,如果你只是整合quartz,這一步不需要我提醒了吧。

<!-- 啟動spring -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<!--你專案下的web.xml檔案應該都有類似的程式碼-->
	<param-value>classpath:spring/spring*.xml</param-value>
</context-param>

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3.建立任務處理MyQuartzTask.java類,比如對資料的增刪改查等都是可以的,本人隨便寫了兩個方法,一個是簡單觸發器,一個是任務觸發器。

package com.baidu;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyQuartzTask {
    //簡單觸發器執行的任務
    public void doSimpleTask(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("doSimpleTask正在執行..."+sdf.format(new Date()));
    }
    
    //cron任務觸發器執行的任務
    public void doCronTask(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("doCronTask正在執行..."+sdf.format(new Date()));
    }
}

4.在spring-quartz.xml中配置quartz定時器,下面分別是簡單觸發器和任務觸發器的配置,根據自己的專案情況任選一種。

簡單觸發器配置程式碼如下:

<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
     <!--注入任務處理類bean  -->   
     <bean id="quartzTask" class="com.baidu.MyQuartzTask"></bean> 
     
     <!-- 1.簡單觸發器任務詳細資訊bean -->   
     <bean id="myJobDetail1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
     	<!-- 設定任務執行物件 -->
     	<property name="targetObject" ref="quartzTask"></property>
     	<!-- 設定任務執行物件中對應的執行方法 -->
     	<property name="targetMethod" value="doSimpleTask"></property>
     	<!-- 設定任務是否可併發執行,預設為不併發 -->
     	<property name="concurrent" value="false"></property>
     </bean>
     
     <!-- 2.制定簡單觸發器 -->  
     <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
     	<!-- 設定任務詳細資訊 -->
     	<property name="jobDetail" ref="myJobDetail1"></property>
     	<!-- 設定延遲1秒開始執行,自己看情況 -->
     	<property name="startDelay" value="1000"></property>
     	<!-- 設定任務執行間隔,本人設定2秒執行一次  -->
     	<property name="repeatInterval" value="2000"></property>
     </bean> 
     
     <!-- 設定觸發器排程工廠 -->   
     <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
     	<property name="triggers">
     		<list>
     			<!-- 觸發器排程工廠排程簡單觸發器 -->
     			<ref bean="simpleTrigger"/>
     			<!--<ref bean=""> 多個觸發器可以這樣配置-->
     		</list>	
     	</property>
     </bean> 
</beans>

cron任務觸發器配置程式碼:

<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
     <!--注入任務處理類bean  -->   
     <bean id="quartzTask" class="com.baidu.MyQuartzTask"></bean> 
     
     <!-- 2.任務觸發器詳細資訊bean -->
     <bean id="myJobDetail2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
     	<!-- 設定任務執行物件 -->
     	<property name="targetObject" ref="quartzTask"></property>
     	<!-- 設定任務執行物件中對應的執行方法 -->
     	<property name="targetMethod" value="doCronTask"></property>
     	<!-- 設定任務是否可併發執行,預設為不併發 -->
     	<property name="concurrent" value="false"></property>
     </bean>
     
     <!-- 2.任務觸發器 -->
     <bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
     	<!-- 設定任務詳細資訊 -->
     	<property name="jobDetail" ref="myJobDetail2"></property>
     	<!-- 設定quartz任務執行表示式 ,每隔三秒執行一次任務-->
     	<property name="cronExpression" value="0/3 * * * * ?"></property>
     </bean>
     
     <!-- 設定觸發器排程工廠 -->   
     <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
     	<property name="triggers">
     		<list>
     			<!-- 觸發器排程工廠排程簡單觸發器 -->
     			<ref bean="cronTrigger2"/>
			<!--<rel bean=""> 多個觸發器可以這樣配置-->
     		</list>	
     	</property>
     </bean> 
</beans>

5.大功告成!