Quartz與Spring強強聯手,定時任務實現更容易

Quartz與Spring強強聯手,定時任務實現更容易
 
環境:
Spring 2.5.4
Quartz 1.6.0
 
Quartz是一個企業級的定時任務執行工具,使用起來也至關容易。可是也有點約束----每一個做業必須實現Job接口。
 
Spring早在1.0就對Quartz提供了支持。Spring AOP的強大功能能夠將這個無聊的事情見鬼去吧。咱們甚至能夠將任何一個普通類的方法設定爲定時執行的方法。而且在Spring初始化的自動啓動定時器(不須要你去寫Main方法),在Spring關閉的時候結束定時器的運行,一避免應用服務器都關閉了,定時器還在後臺默默無聞「空轉」。呵呵,下面就看個例子吧:
 
目標:將一個普通的業務方法做爲定時做業的方法經過Spring配置去執行。
 
代碼:
 
第一步:寫業務方法
 
package stu.quartz;

/**
* 業務方法
*
* @author leizhimin,2008-10-8 15:41:52
*/

public class TestService {
         public void doSomething() {
                System.out.println( "Do Something Freely!");
        }

         public void justDoIt() {
                System.out.println( "Just Do It!");
        }
}
 
2、配置Spring
 
<? xml version ="1.0" encoding ="UTF-8" ?>
< beans default-autowire ="byName" xmlns ="http://www.springframework.org/schema/beans"
             xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation ="http://www.springframework.org/schema/beans [url]http://www.springframework.org/schema/beans/spring-beans.xsd[/url]" >

        <!-- 普通的業務Bean -->
         < bean name ="testService" class ="stu.quartz.TestService" />

        <!-- 做業 -->
         < bean id ="jobDetail_test" class ="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
                 < property name ="targetObject" ref ="testService" />
                 < property name ="targetMethod" value ="justDoIt" />

         </ bean >

        <!-- 觸發器 -->
         < bean name ="testTrigger" class ="org.springframework.scheduling.quartz.SimpleTriggerBean" >
                 < property name ="jobDetail" ref ="jobDetail_test" />
                 < property name ="startDelay" value ="10" />
                 < property name ="repeatInterval" value ="2000" />
                 < property name ="repeatCount" value ="100" />
                 < property name ="jobDataAsMap" >
                         < map >
                                 < entry key ="count" value ="10" />
                         </ map >
                 </ property >
         </ bean >

        <!-- 計劃 -->
         < bean name ="testScheduler" class ="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
                 < property name ="triggers" >
                         < list >
                                 < ref bean ="testTrigger" />
                         </ list >
                 </ property >
                 < property name ="schedulerContextAsMap" >
                         < map >
                                 < entry key ="timeout" value ="30" />
                         </ map >
                 </ property >

                <!-- Quartz的高級配置-->
                <!-- <property name="configLocation" value="classpath:com/lavasoft/quartz.properties"/>-->
         </ bean >

</ beans >
 
3、寫測試
 
package stu.quartz;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* 測試類
*
* @author leizhimin,2008-10-8 16:33:21
*/

public class TestQuartz {
         public static void main(String[] args) {
                ApplicationContext context = new ClassPathXmlApplicationContext( "/Application_quartz.xml");
                System.out.println( "Main方法執行開始了! 定時器伴隨着Spring的初始化執行了。。。");

                System.out.println( "Main方法執行結束了!");
        }
}
 
運行測試:
Main方法執行開始了! 定時器伴隨着Spring的初始化執行了。。。
Main方法執行結束了!
Just Do It!
Just Do It!
Just Do It!
Just Do It!
Just Do It!
......
 
運行結果表名:在Spring初始化後,定時器根據延遲時間啓動執行,而且在執行過過程當中,Main線程一直沒有退出。定時器線程一直在執行。
 
在強行終止Main線程的時候,定時器也中止執行。
相關文章
相關標籤/搜索