使用Quartz2.1.6+Spring3.0實現任務調度簡單樣本

來源:互聯網
上載者:User

環境:Myeclipse10+JDK1.6+Spring3.0(Myeclipse內建的) +Quartz2.1.6+Tomcat7 

因為項目中要用到任務調度,如果使用Spring內建的定時器的話,配置調度規則時不夠靈活。頭決定使用Quartz,發了倆連結供我參考,從那倆連結對Quartz有了初步的認識,但是那倆連結都是針對1.6版本的,1.8版本後有了大的改變,包括初始化方法都發生了改變。參照了網上的文章並閱讀了官方的文檔調試了將近兩天依然沒法調通,官方的文檔並不太相近,樣本也並不豐富。

 

後來朋友給我遠端偵錯了下,問題解決了。

解決過程中遇到過以下問題: 

1.web.xml中關於Spring設定檔配置的順序s問題,Spring的設定檔位置制定必須放在其他Spring其他配置之前。

2.缺少 commons-collections-3.2.jar 因為會用到org/apache/commons/collections下面的某個類,而我的項目中沒有。這個要從網上下。

3.applicationContext.xml中關於Job的配置,網上有些文章中,實際工作類是繼承於Spring的某個類,這個類實現了Quartz的Job介面,但是按照那個寫法,和對應的設定檔,無法工作,後來用了朋友給的方法,工作類就是普通的java類,在設定檔中制定具體的方法,設定檔稍後貼上。

 

步驟:

1:在Myeclipse中建立Web項目,建立完畢後在項目上點右鍵 ,Myeclipse、Add Spring Capabilities,選擇要添加的包,我把帶Spring字樣的都選上了,其實用不到這麼多。

2:複製下載Quartz-2.16解壓後根目錄中quartz-all-2.1.6.jar和lib目錄中的所有jar包,在項目上右鍵,粘貼,相同方法再添加 commons-collections-3.2.jar  到項目中,再在項目上右鍵、properties, Java Build Path,Add JARs,選擇剛添加的那些jar包,將這些jar包引用到項目中。(你也可以不將這些jar包複製到項目中,而直接在java build path介面中添加外部的,但是為了便於項目的複製,我一般都這麼做)

3.編寫工作的類。代碼很簡單:

 

/* 
 * @author sixi 
 * @version 0.1 
 * Company:Tsinghua
 * Date:2012-09-15
 * Description:這是一個執行調度的具體類,在Spring設定檔中指定調用此類的work方法
 * */
package com.tsinghua.test;

import java.util.Date;

/*
 * 使用spring+Quartz執行任務調度的具體類
 * */
public class MyJob {

    /*
     * Description:具體工作的方法,此方法只是向控制台輸出目前時間,
     * 輸入的日誌在:%tomcatRoot%\logs\tomcat7-stdout.yyyy-MM-dd.log中,
     * 其中,yyyy-MM-dd為部署的日期,經實驗發現預設情況下並不是每天都產生一個stdout的記錄檔
     * @return 返回void
     * */
    public void work()
    {
         System.out.println("目前時間:"+new Date().toString());  
    }
}//End of MyJob

 

 4.配置web.xml,具體設定檔如下:

 

 <?xml version="1.0" encoding="UTF-8"?> 

      <web-app version="3.0" 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/web-app_3_0.xsd">
    <display-name>SpringQuartzDemo</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- 說明:Spring的設定檔設置必須在啟動Spring Bean工廠監聽之前,否則會報錯, .Net設定檔好像沒有先後順序 -->

    <!-- Spring設定檔開始 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
    </context-param>
    <!-- Spring設定檔結束 -->

    <!-- 啟動 Spring Bean 工廠監聽開始 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 啟動 Spring Bean 工廠監聽結束 -->
</web-app>

 

 5.配置applicationContext.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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- 工作的bean -->
    <bean id="myJob" class="com.tsinghua.test.MyJob" />

    <!-- job的配置開始 -->
    <bean id="myJobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="myJob" />
        </property>
        <property name="targetMethod">
            <value>work</value>
        </property>
    </bean>
    <!-- job的配置結束 -->

    <!-- 調度的配置開始 -->
    <bean id="crontestJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="myJobDetail" />
        </property>
        <property name="cronExpression">
            <value>0/1 * * * * ?</value>
        </property>
    </bean>
    <!-- 調度的配置結束 -->

    <!-- 啟動觸發器的配置開始 -->
    <bean name="startQuertz" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="crontestJobTrigger" />

            </list>
        </property>
    </bean>
    <!-- 啟動觸發器的配置結束 -->
</beans>  

 

 6.在Tomcat中調試,即可在Console視窗看到輸出

 

項目代碼:

 SpringQuartzDemo

 

PS:今天部落格園插入程式碼片段的時候怎麼老是出問題,不是代碼的最上面一行就是代碼的最下面一行會跑到程式碼片段外面去了。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.