<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    無線&移動互聯網技術研發

    換位思考·····
    posts - 19, comments - 53, trackbacks - 0, articles - 283
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Quartz 與 Spring 集成使用的實例

    Posted on 2010-05-28 01:13 Gavin.lee 閱讀(925) 評論(0)  編輯  收藏 所屬分類: SSH2 --Spring
    在前面文章中,有舉出不集成但用Quartz的應用,這里,我們通過Spring 的IOC來與Quartz集成使用,對于定時任務,我們可以讓這個應用做為jar 小工具在linux下跑,也可以將應用單獨放在一個容器里跑。這個視情況而定
    一下是一個簡單的應用,quartz + Spring 集成使用的核心就這Spring的配置文件中了
    <?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:aop
    ="http://www.springframework.org/schema/aop" 
        xmlns:tx
    ="http://www.springframework.org/schema/tx"
        xsi:schemaLocation
    ="http://www.springframework.org/schema/beans         
            http://www.springframework.org/schema/beans/spring-beans-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/aop   
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
            http://www.springframework.org/schema/tx   
            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
    >

        
    <!-- *************************** 工作任務調度 *************************** -->

        
    <!-- 要調用的工作類 -->
        
    <bean id="quartzJob_common" class="com.quartz.job.DoJobMethod"></bean>

        
    <!-- 可繼續加新的任務   -->
        
    <!-- 要調用的工作類結束 -->

        
    <!-- 定義調用對象和調用對象的方法 -->
        
    <bean id="jobtask1"
            class
    ="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            
    <!-- 調用的類 -->
            
    <property name="targetObject">
                
    <ref bean="quartzJob_common" />
            
    </property>
            
    <!-- 調用類中的方法 -->
            
    <property name="targetMethod">
                
    <value>doMethod1</value>
            
    </property>
        
    </bean>

        
    <bean id="jobtask2"
            class
    ="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            
    <!-- 調用的類 -->
            
    <property name="targetObject">
                
    <ref bean="quartzJob_common" />
            
    </property>
            
    <!-- 調用類中的方法 -->
            
    <property name="targetMethod">
                
    <value>doMethod2</value>
            
    </property>
        
    </bean>
        
    <!-- 可繼續加新的   -->
        
    <!-- 定義調用對象和調用對象的方法結束 -->


        
    <!-- 定義觸發時間 -->
        
    <bean id="doTime1" class="org.springframework.scheduling.quartz.CronTriggerBean">
            
    <property name="jobDetail">
                
    <ref bean="jobtask1" />
            
    </property>
            
    <!-- cron表達式 此處定義為每天零辰00:15執行任務 -->
            
    <property name="cronExpression">
                
    <value>0 15 00 ? * *</value>
                
    <!--<value>10 * * ? * *</value>-->
            
    </property>
        
    </bean>
        
    <bean id="doTime2" class="org.springframework.scheduling.quartz.CronTriggerBean">
            
    <property name="jobDetail">
                
    <ref bean="jobtask2" />
            
    </property>
            
    <!-- cron表達式   此處定義每1分鐘觸發一次-->
            
    <property name="cronExpression">
                
    <value>0 */1 * ? * *</value>
            
    </property>
        
    </bean> 

        
    <bean id="doTime3" class="org.springframework.scheduling.quartz.CronTriggerBean">
            
    <property name="jobDetail">
                
    <ref bean="jobtask3" />
            
    </property>
            
    <!-- cron表達式   此處定義每天上午10:30和晚上22:20觸發 即每半天觸發一次-->
            
    <property name="cronExpression">
                
    <value>0 30,20 10,22 ? * *</value>
                
    <!--<value>10 * * ? * *</value> -->
            
    </property>
        
    </bean>
        
    <!-- 可繼續加新的   -->
        
    <!-- 定義觸發時間結束 -->


        
    <!-- 總管理類 如果將lazy-init='false'那么容器啟動就會執行調度程序   -->
        
    <bean id="start_common" lazy-init="false" autowire="no"    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            
    <property name="triggers">
                
    <list>
                    
    <ref bean="doTime1" />
                    
    <ref bean="doTime2" />
                    
    <!-- 可繼續加新的   -->

                
    </list>
            
    </property>
        
    </bean>
        
    <!-- 總管理類結束   -->
    </beans>

    下面是一個調度器的入口(多線程)
    package com.quartz.job;

    /**
     * 任務調度的其中一個入口
     * 這個入口類需要這Spring配置文件contextApplication.xml中定義
     * 這個入口類可以定義一個或多個
     * 該類里的方法,將會有單獨的線程來運行
     * 
    @author Administrator
     *
     
    */

    public class DoJobMethod {

        
    public void doMethod1() {
            System.out.println(
    "我是任務調度的第一個方法");
        }

        
        
    public void doMethod2() {
            System.out.println(
    "我是任務調度的第二個方法");
        }

    }


    后話就不用多說了,要這web.xml里配置啟動加載spring配置文件
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" 
        xmlns
    ="http://java.sun.com/xml/ns/j2ee" 
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation
    ="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    >
      
          
    <listener>
            
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        
    </listener>    
        
    <context-param>
            
    <param-name>contextConfigLocation</param-name>
            
    <param-value>/WEB-INF/applicationContext.xml</param-value>
        
    </context-param>
    </web-app>

    這里要說一個小事,我們經常在這里定義的觸發器條件都是比較有規律的,如果你想做到在容器初始化時候調用一下這個任務,就需要一個過濾器來監聽容器,用以初始化。也問了前輩,好像還沒有什么更好的辦法,如果你有的話,希望不惜賜教

    PS:剛想到一個辦法,在spring容器初始化這個jobbean時候,指定一個init-method。在這個方法里調用其他的任務方法,這樣可以簡單解決容器初始化時候做任務
    1. 指定init-method
    <!-- 要調用的工作類 -->
        
    <bean id="quartzJob_common" class="com.quartz.job.DoJobMethod" init-method="doMethod"></bean>

    2. 這init-method里調用任務方法
    public class DoJobMethod {

        
    public void doMethod() {
            doMethod1();
            doMethod2();
        }

        
        
    public void doMethod1() {
            System.out.println(
    "我是任務調度的第一個方法");
        }

        
        
    public void doMethod2() {
            System.out.println(
    "我是任務調度的第二個方法");
        }

    }
    主站蜘蛛池模板: 精品在线免费观看| 亚欧免费视频一区二区三区| 成年女人毛片免费观看97| 波多野结衣免费在线观看| 午夜亚洲AV日韩AV无码大全| 亚洲一区在线视频| 精品熟女少妇AV免费观看| 久久夜色精品国产噜噜亚洲a| 一个人看的hd免费视频| 区久久AAA片69亚洲| 久久性生大片免费观看性| 亚洲中文字幕无码一区二区三区| 成人无码区免费A∨直播| 国产亚洲精AA在线观看SEE| 久久午夜夜伦鲁鲁片免费无码| 国产麻豆剧传媒精品国产免费| 亚洲中文字幕在线乱码| 日本黄色动图免费在线观看| 久久精品国产亚洲av麻豆小说| 四虎成年永久免费网站| 成年人视频在线观看免费| 久久精品国产亚洲AV电影网| 亚洲国产a级视频| 在线综合亚洲中文精品| 国产色婷婷精品免费视频| 一级黄色毛片免费看| 五月天网站亚洲小说| 无码日韩精品一区二区免费| 亚洲成在人线在线播放无码 | 中文字幕日韩亚洲| 亚洲精品无码成人| 亚洲综合精品网站在线观看| 亚洲色成人WWW永久在线观看| 免费国产污网站在线观看15| 亚洲人成免费电影| 国产精品免费精品自在线观看| 亚洲人成色777777老人头| 亚洲性猛交XXXX| 无码免费午夜福利片在线| 一级毛片**免费看试看20分钟 | 国产亚洲精aa在线看|