Spring Quartz 任務調度

1、Quartz做業類的繼承方式來說,能夠分爲兩類:php

  1. 做業類須要繼承自特定的做業類基類,如Quartz中須要繼承自org.springframework.scheduling.quartz.QuartzJobBean;java.util.Timer中須要繼承自java.util.TimerTask。
  2. 做業類即普通的java類,不須要繼承自任何基類。

注:推薦使用第二種方式,由於這樣因此的類都是普通類,不須要事先區別對待。java

  • 從任務調度的觸發時機來分,這裏主要是針對做業使用的觸發器,主要有如下兩種:
  1. 每隔指定時間則觸發一次,在Quartz中對應的觸發器爲:org.springframework.scheduling.quartz.SimpleTriggerBean
  2. 每到指定時間則觸發一次,在Quartz中對應的調度器爲:org.springframework.scheduling.quartz.CronTriggerBean

注:並不是每種任務均可以使用這兩種觸發器,如java.util.TimerTask任務就只能使用第一種。Quartz和spring task均可以支持這兩種觸發條件。spring

二 、第一種,做業類繼承自特定的基類:org.springframework.scheduling.quartz.QuartzJobBeantomcat

第一步:定義做業類併發

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class Job1 extends QuartzJobBean {
private int timeout;
private static int i = 0 ;
//調度工廠實例化後,通過timeout時間開始執行調度
public void setTimeout( int timeout) {
this .timeout = timeout;
}
/**
* 要調度的具體任務
*/
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
System.out.println( "定時任務執行中…" );
}
}

  第二步:spring配置文件中配置做業類JobDetailBean ide

1
2
3
4
5
6
7
8
<bean name= "job1" class = "org.springframework.scheduling.quartz.JobDetailBean" >
<property name= "jobClass" value= "com.gy.Job1" />
<property name= "jobDataAsMap" >
<map>
<entry key= "timeout" value= "0" />
</map>
</property>
</bean>

 說明:org.springframework.scheduling.quartz.JobDetailBean有兩個屬性,jobClass屬性即咱們在java代碼中定義的任務類,jobDataAsMap屬性即該任務類中須要注入的屬性值。 工具

第三步:配置做業調度的觸發方式(觸發器)post

Quartz的做業觸發器有兩種,分別是this

org.springframework.scheduling.quartz.SimpleTriggerBeanspa

org.springframework.scheduling.quartz.CronTriggerBean

第一種 SimpleTriggerBean,只支持按照必定頻度調用任務,如每隔30分鐘運行一次。配置方式以下:

1
2
3
4
5
<bean id= "simpleTrigger" class = "org.springframework.scheduling.quartz.SimpleTriggerBean" >
<property name= "jobDetail" ref= "job1" />
<property name= "startDelay" value= "0" /><!– 調度工廠實例化後,通過 0 秒開始執行調度 –>
<property name= "repeatInterval" value= "2000" /><!– 每 2 秒調度一次 –>
</bean>

第二種 CronTriggerBean,支持到指定時間運行一次,如天天12:00運行一次等。配置方式以下:

1
2
3
4
5
<bean id= "cronTrigger" class = "org.springframework.scheduling.quartz.CronTriggerBean" >
<property name= "jobDetail" ref= "job1" />
<!—天天 12 : 00 運行一次 —>
<property name= "cronExpression" value= "0 0 12 * * ?" />
</bean>

第四步:配置調度工廠

1
2
3
4
5
6
7
<bean class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name= "triggers" >
<list>
<ref bean= "cronTrigger" />
</list>
</property>
</bean>

說明:該參數指定的就是以前配置的觸發器的名字。

第五步:啓動你的應用便可,即將工程部署至tomcat或其餘容器。

3、第二種,做業類不繼承特定基類。

Spring可以支持這種方式,歸功於兩個類:

org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean

org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean

這兩個類分別對應spring支持的兩種實現任務調度的方式,即前文提到到java自帶的timer task方式和Quartz方式。這裏我只寫MethodInvokingJobDetailFactoryBean的用法,使用該類的好處是,咱們的任務類再也不須要繼承自任何類,而是普通的pojo。

第一步:編寫任務類

1
2
3
4
5
public class Job2 {
public void doJob2() {
System.out.println( "不繼承QuartzJobBean方式-調度進行中…" );
}
}

說明:能夠看出,這就是一個普通的類,而且有一個方法。

第二步:配置做業類

1
2
3
4
5
6
7
8
<bean id= "job2"
class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
<property name= "targetObject" >
<bean class = "com.gy.Job2" />
</property>
<property name= "targetMethod" value= "doJob2" />
<property name= "concurrent" value= "false" /><!– 做業不併發調度 –>
</bean>

說明:這一步是關鍵步驟,聲明一個MethodInvokingJobDetailFactoryBean,有兩個關鍵屬性:targetObject指定任務類,targetMethod指定運行的方法。往下的步驟就與方法一相同了,爲了完整,一樣貼出。

第三步:配置做業調度的觸發方式(觸發器)

Quartz的做業觸發器有兩種,分別是

org.springframework.scheduling.quartz.SimpleTriggerBean

org.springframework.scheduling.quartz.CronTriggerBean

第一種SimpleTriggerBean,只支持按照必定頻度調用任務,如每隔30分鐘運行一次。配置方式以下:

1
2
3
4
5
<bean id= "simpleTrigger" class = "org.springframework.scheduling.quartz.SimpleTriggerBean" >
<property name= "jobDetail" ref= "job2" />
<property name= "startDelay" value= "0" /><!– 調度工廠實例化後,通過 0 秒開始執行調度 –>
<property name= "repeatInterval" value= "2000" /><!– 每 2 秒調度一次 –>
</bean>

第二種CronTriggerBean,支持到指定時間運行一次,如天天12:00運行一次等。配置方式以下:

1
2
3
4
5
ronTriggerBean">
<property name= "jobDetail" ref= "job2" />
<!—天天 12 : 00 運行一次 —>
<property name= "cronExpression" value= "0 0 12 * * ?" />
</bean>

以上兩種調度方式根據實際狀況,任選一種便可。

第四步:配置調度工廠

1
2
3
4
5
6
7
<bean class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name= "triggers" >
<list>
<ref bean= "cronTrigger" />
</list>
</property>
</bean>

說明:該參數指定的就是以前配置的觸發器的名字。

第五步:啓動你的應用便可,即將工程部署至tomcat或其餘容器。  

到此,spring中Quartz的基本配置就介紹完了,固然了,使用以前,要導入相應的spring的包與Quartz的包,這些就不消多說了。

其實能夠看出Quartz的配置看上去仍是挺複雜的,沒有辦法,由於Quartz實際上是個重量級的工具,若是咱們只是想簡單的執行幾個簡單的定時任務,有沒有更簡單的工具,有!

posted @ 2016-07-05 09:32 獨具匠心

  據說過有註解的定時任務簡單不少

本人本身的 完整的 Quartz XML配置


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!--- 觸發器的bean的設置,在這裏咱們設置了咱們要觸發的jobDetail是哪一個 -->
    
    
    <!-- 定時修改狀態 -->
	<!-- 定時器類 -->
	<bean id="updateOffine" class="com.zte.tirgger.updateOffine">
	</bean>
	
	<!-- 任務調度 -->
	<bean id="updateOffineJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<!-- 調用的類 -->
		<property name="targetObject">
			<ref bean="updateOffine"/>
		</property>
		<!-- 調用類中的方法 -->
		<property name="targetMethod">
			<value>test</value>
		</property>
	</bean>
	
	<!-- 調度計劃 -->
	<bean id="updateOffineTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<property name="jobDetail">
			<ref bean="updateOffineJob"/>
		</property>
		<!-- 首次執行延期5分鐘 每隔1分鐘執行一次 -->
		<property name="startDelay" value="6000" />
		<property name="repeatInterval" value="6000" />
	</bean>
	
	<bean id="updateOffineJobSchedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">  
            <list>  
                <ref bean="updateOffineTrigger" />
            </list>  
        </property>  
    </bean>  
    
</beans>
複製代碼
相關文章
相關標籤/搜索