Spring - Java/J2EE Application Framework 应用框架 第 18 章 使用Quartz或Timer完成时序调度工作
第?18?章?使用Quartz或Timer完成時序調度工作
18.1.?簡介
Spring提供了支持時序調度(譯者注:Scheduling,下同)的整合類.現在, Spring支持內置于1.3版本以來的JDK中的Timer和Quartz Scheduler(http://www.quartzscheduler.org)。 兩個時序調度器通過FactoryBean建立,保持著可選的對Timers或者Triggers的引用。更進一步的, 對于Quartz Scheduler和Timer兩者存在一個方便的類允許你調用目標對象(類似于通常的MethodInvokingFactoryBeans)上的某個方法
18.2.?使用OpenSymphony Quartz Scheduler
Quartz使用Triggers,Jobs和JobDetail來實現時序調度中的各種工作。 為了了解Quartz背后的種種基本觀點,你可以移步至http://www.opensymphony.com/quartz。 為了方便的使用,Spring提供了幾個類在基于Spring的應用中來簡化對Quartz的使用。
18.2.1.?使用JobDetailBean
JobDetail?對象包括了運行一個job所需要的所有信息。 于是Spring提供了一個所謂的JobDetailBean使得JobDetail擁有了一個真實的,有意義的默認值。讓我們來看個例子:
<bean name="exampleJob" class="org.springframework.scheduling.quartz.JobDetailBean"><property name="jobClass"><value>example.ExampleJob</value></property><property name="jobDataAsMap"><map><entry key="timeout"><value>5</value></entry></map></property> </bean>Job detail bean擁有所有運行job(ExampleJob)的必要信息。通過job的data map來制定timeout。 Job的data map可以通過JobExecutionContext(在運行時刻傳遞給你)來得到, 但是JobDetailBean也把從job的data map中得到的屬性映射到實際job中的屬性中去。 所以,如果ExampleJob中包含一個名為timeout的屬性,JobDetailBean將自動為它賦值:
package example;public class ExampleJob extends QuartzJobBean {private int timeout;/*** Setter called after the ExampleJob is instantiated* with the value from the JobDetailBean (5)*/ public void setTimeout(int timeout) {this.timeout = timeout;}protected void executeInternal(JobExecutionContext ctx)throws JobExecutionException {// do the actual work} }所有Job detail bean中的一些其他的設定對你來說也是可以同樣設置的.
注意:使用name和group屬性,你可以修改job在哪一個組下運行和使用什么名稱。 默認情況下,job的名稱等于job detai bean的名稱(在上面的例子中為exampleJob)。
18.2.2.?使用MethodInvokingJobDetailFactoryBean
通常情況下,你只需要調用特定對象上的一個方法。你可以使用MethodInvokingJobDetailFactoryBean準確的做到這一點:
<bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject"><ref bean="exampleBusinessObject"/></property><property name="targetMethod"><value>doIt</value></property> </bean>上面例子將導致exampleBusinessObject中的doIt方法被調用(如下):
public class BusinessObject {// properties and collaboratorspublic void doIt() {// do the actual work} } <bean id="exampleBusinessObject" class="examples.ExampleBusinessObject"/>使用MethodInvokingJobDetailFactoryBean你不需要創建只有一行代碼且只調用一個方法的job, 你只需要創建真實的業務對象來包裝具體的細節的對象。
默認情況下,Quartz Jobs是無狀態的,可能導致jobs之間互相的影響。如果你為相同的JobDetail指定兩個觸發器, 很可能當第一個job完成之前,第二個job就開始了。如果JobDetail對象實現了Stateful接口,就不會發生這樣的事情。 第二個job將不會在第一個job完成之前開始。為了使得jobs不并發運行,設置MethodInvokingJobDetailFactoryBean中的concurrent標記為false。
<bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject"><ref bean="exampleBusinessObject"/></property><property name="targetMethod"><value>doIt</value></property> </bean>注意:默認情況下,jobs在并行的方式下運行。
18.2.3.?使用triggers和SchedulerFactoryBean來包裝任務
我們已經創建了job details,jobs。我們回顧了允許你調用特定對象上某一個方法的便捷的bean。 當然我們仍需要調度這些jobs。這需要使用triggers和SchedulerFactoryBean來完成。 Quartz自帶一些可供使用的triggers。Spring提供兩個子類triggers,分別為CronTriggerBean和SimpleTriggerBean。
Triggers也需要被調度。Spring提供SchedulerFactoryBean來暴露一些屬性來設置triggers。SchedulerFactoryBean負責調度那些實際的triggers。
兩個例子:
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"><property name="jobDetail"><!-- see the example of method invoking job above --> <ref bean="methodInvokingJobDetail"/></property><property name="startDelay"><!-- 10 seconds --><value>10000</value></property><property name="repeatInterval"><!-- repeat every 50 seconds --><value>50000</value></property> </bean><bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail"><ref bean="exampleJob"/></property><property name="cronExpression"><!-- run every morning at 6 am --><value>0 6 * * 1</value></property> </bean>現在我們創建了兩個triggers,其中一個開始延遲10秒以后每50秒運行一次,另一個每天早上6點鐘運行。 我們需要創建一個SchedulerFactoryBean來最終實現上述的一切:
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref local="cronTrigger"/><ref local="simpleTrigger"/></list></property> </bean>更多的一些屬性你可以通過SchedulerFactoryBean來設置,例如job details使用的Calendars,用來訂制Quartz的一些屬性以及其它。 你可以看相應的JavaDOC(http://www.springframework.org/docs/api/org/springframework/scheduling/quartz/SchedulerFactoryBean.html)來了解進一步的信息。
18.3.?使用JDK Timer支持類
另外一個調度任務的途徑是使用JDK Timer對象。更多的關于Timers的信息可以在這里http://java.sun.com/docs/books/tutorial/essential/threads/timer.html找到。 上面討論的概念仍可以應用于Timer的支持。你可以創建定制的timer或者調用某些方法的timer。 包裝timers的工作由TimerFactoryBean完成。
18.3.1.?創建定制的timers
你可以使用TimerTask創建定制的timer tasks,類似于Quartz中的jobs:
public class CheckEmailAddresses extends TimerTask {private List emailAddresses;public void setEmailAddresses(List emailAddresses) {this.emailAddresses = emailAddresses;}public void run() {// iterate over all email addresses and archive them} }包裝它是簡單的:
<bean id="checkEmail" class="examples.CheckEmailAddress"><property name="emailAddresses"><list><value>test@springframework.org</value><value>foo@bar.com</value><value>john@doe.net</value></list></property> </bean><bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"><!-- wait 10 seconds before starting repeated execution --><property name="delay"><value>10000</value></property><!-- run every 50 seconds --><property name="period"><value>50000</value></property><property name="timerTask"><ref local="checkEmail"/></property> </bean>18.3.2.?使用MethodInvokingTimerTaskFactoryBean
就像Quartz的支持一樣,Timer的支持也有一個組件允許你周期性地調用一個方法:
<bean id="methodInvokingTask" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean"><property name="targetObject"><ref bean="exampleBusinessObject"/></property><property name="targetMethod"><value>doIt</value></property> </bean>上面的例子將會導致exampleBusinessObject上的doIt方法被調用(如下):
public class BusinessObject {// properties and collaboratorspublic void doIt() {// do the actual work} }把上面例子中提到ScheduledTimerTask的引用改為methodInvokingTask將導致該task被執行。
18.3.3.?包裝:使用TimerFactoryBean來建立tasks
TimerFactoryBean類似于QuartzSchedulerFactoryBean,都是服務于一個目的:建立起實際的時序調度。 TimerFactoryBean建立一個實際的Timer來調度它引用的那些tasks。你可以指定它是否使用一個守護線程。
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean"><property name="scheduledTimerTasks"><list><!-- see the example above --><ref local="scheduledTask"/></list></property> </bean>就是這些了!
from:?http://docs.huihoo.com/spring/zh-cn/scheduling.html
總結
以上是生活随笔為你收集整理的Spring - Java/J2EE Application Framework 应用框架 第 18 章 使用Quartz或Timer完成时序调度工作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring - Java/J2EE A
- 下一篇: Spring - Java/J2EE A