Quartz學習(七)--Spring整合Quartz(MethodInvokingJobDetailFactoryBean方式)

一、該方式利弊

  該方式通過MethodInvokingJobDetailFactoryBean在運行中動態生成,需要配置執行任務的目標類、目標方法。但是這種方法動態生成的JobBean不支持序列號,也就是說Job不能存到持久化

  該方式通常用於調用特定對象的一個方法。不用創建單獨的job對象,只需要建立正常的業務對象,用這樣方式去調用其中的一個方法。
  該方式通過透明的使用Quartz達到定時任務開發的目的,總體說第二種對開發人員更方便!


二、整合方式二示例步驟

  1、jar導入

    導入Spring的核心jar包和quartz.jar以及Spring-context-support.jar

 

  2、編寫job類(不用繼承任何父類)

    該類不需要繼承任何父類,也不需要實現任何接口,只是一個普通的java類

public class AutoTask 
{
    public void checkLine()
  {	
      //我們的任務執行體
    System.out.println("執行任務1");
  }

   public void  checkClient()
  {	
      //我們的任務執行體
    System.out.println("執行任務2");
   }

 }


3、編寫quartz.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"
        default-lazy-init="false">


     <!-- task -->
    <!-- 定義了一個任務JobDetail -->
    <bean id="quartzLine" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="autoReconciliation" />
         <property name="targetMethod" value="checkLine" />
    </bean>
    
    <bean id="quartzClient" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="autoReconciliation" />
         <property name="targetMethod" value="checkClient" />
    </bean>

    <!-- 第二步 調度定時任務 的觸發器-->
    <!-- 這種配置可以精確幾點執行定時任務 -->
    <bean id="cronQuartzLine" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="quartzLine"></ref>
        </property>
        <property name="cronExpression">
            <value>${lineTime}</value>
        </property>
    </bean>
    
    <bean id="cronQuartzClient" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="quartzClient"></ref>
        </property>
        <property name="cronExpression">
            <value>${hanlderTime}</value>
        </property>
     <!--<property name="cronExpression" value="*/1 * * * * ?"/> 每隔1秒鐘觸發一次 -->  
    </bean>

    <!--第三步 啓動定時任務的調度器,注意這裏的ref bean -->
    <bean id="schedulerFactoryBean" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronQuartzLine"></ref>
                <ref bean="cronQuartzClient"></ref>
            </list>
        </property>
         <!--  <property name="configLocation" value="classpath:quartz.properties"/>-->
    </bean>

   <!--這個地方你作業的實現    -->
    <bean id="autoReconciliation" class="com.huateng.hbmis.task.AutoTask" />
</beans>

說明:

1)Cron表達式的格式:秒 分 時 日 月 周 年(可選)。

字段名              允許的值                   允許的特殊字符  

秒                  0-59                      , - * /  
分                  0-59                      , - * /  
小時                0-23                      , - * /  
日                 1-31                     , - * ? / L W C  
月                 1-12 or JAN-DEC          , - * /  
周幾               1-7 or SUN-SAT          , - * ? / L C #  
年 (可選字段)     empty, 1970-2099           , - * /

        

      “?”字符:表示不確定的值

       “,”字符:指定數個值

       “-”字符:指定一個值的範圍

       “/”字符:指定一個值的增加幅度。n/m表示從n開始,每次增加m

       “L”字符:用在日表示一個月中的最後一天,用在周表示該月最後一個星期X

       “W”字符:指定離給定日期最近的工作日(週一到週五)

       “#”字符:表示該月第幾個周X。6#3表示該月第3個週五

 2)Cron表達式範例:

        每隔5秒執行一次:*/5 * * * * ?

        每隔1分鐘執行一次:0 */1 * * * ?

        每天23點執行一次:0 0 23 * * ?

        每天凌晨1點執行一次:0 0 1 * * ?

        每月1號凌晨1點執行一次:0 0 1 1 * ?

        每月最後一天23點執行一次:0 0 23 L * ?

        每週星期天凌晨1點實行一次:0 0 1 ? * L

        在26分、29分、33分執行一次:0 26,29,33 * * * ?

       每天的0點、13點、18點、21點都執行一次:0 0 0,13,18,21 * * ?
  

4、讓容器加載quartz.xml

  在web.xml中添加:

   <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/quartz.xml</param-value>
     </context-param>


  ###其實quartz.xml文件的內容完全可以寫在applicationContext.xml中的,不過那樣會顯得雜亂。

三、注意事項

在Spring配置和Quartz集成內容時,有兩點需要注意

           1、在<Beans>中不能夠設置default-lazy-init="true",否則定時任務不觸發,如果不明確指明default-lazy-init的值,默認是false。

           2、在<Beans>中不能夠設置default-autowire="byName"的屬性,否則後臺會報org.springframework.beans.factory.BeanCreationException錯誤,這樣就不能通過Bean名稱自動注入,必須通過明確引用注入。



發佈了27 篇原創文章 · 獲贊 22 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章