Spring提供了支持時(shí)序調度(譯者注:Scheduling,下同)的整合類(lèi).現在, Spring支持內置于1.3版本以來(lái)的JDK中的Timer和Quartz Scheduler(http://www.quartzscheduler.org)。 兩個(gè)時(shí)序調度器通過(guò)FactoryBean建立,保持著(zhù)可選的對Timers或者Triggers的引用。更進(jìn)一步的, 對于Quartz Scheduler和Timer兩者存在一個(gè)方便的類(lèi)允許你調用目標對象(類(lèi)似于通常的MethodInvokingFactoryBeans)上的某個(gè)方法
Quartz使用Triggers,Jobs和JobDetail來(lái)實(shí)現時(shí)序調度中的各種工作。 為了了解Quartz背后的種種基本觀(guān)點(diǎn),你可以移步至http://www.opensymphony.com/quartz。 為了方便的使用,Spring提供了幾個(gè)類(lèi)在基于Spring的應用中來(lái)簡(jiǎn)化對Quartz的使用。
JobDetail 對象包括了運行一個(gè)job所需要的所有信息。 于是Spring提供了一個(gè)所謂的JobDetailBean使得JobDetail擁有了一個(gè)真實(shí)的,有意義的默認值。讓我們來(lái)看個(gè)例子:
<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)的必要信息。通過(guò)job的data map來(lái)制定timeout。 Job的data map可以通過(guò)JobExecutionContext(在運行時(shí)刻傳遞給你)來(lái)得到, 但是JobDetailBean也把從job的data map中得到的屬性映射到實(shí)際job中的屬性中去。 所以,如果ExampleJob中包含一個(gè)名為timeout的屬性,JobDetailBean將自動(dòng)為它賦值:
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中的一些其他的設定對你來(lái)說(shuō)也是可以同樣設置的.
注意:使用name和group屬性,你可以修改job在哪一個(gè)組下運行和使用什么名稱(chēng)。 默認情況下,job的名稱(chēng)等于job detai bean的名稱(chēng)(在上面的例子中為exampleJob)。
通常情況下,你只需要調用特定對象上的一個(gè)方法。你可以使用MethodInvokingJobDetailFactoryBean準確的做到這一點(diǎn):
<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你不需要創(chuàng )建只有一行代碼且只調用一個(gè)方法的job, 你只需要創(chuàng )建真實(shí)的業(yè)務(wù)對象來(lái)包裝具體的細節的對象。
默認情況下,Quartz Jobs是無(wú)狀態(tài)的,可能導致jobs之間互相的影響。如果你為相同的JobDetail指定兩個(gè)觸發(fā)器, 很可能當第一個(gè)job完成之前,第二個(gè)job就開(kāi)始了。如果JobDetail對象實(shí)現了Stateful接口,就不會(huì )發(fā)生這樣的事情。 第二個(gè)job將不會(huì )在第一個(gè)job完成之前開(kāi)始。為了使得jobs不并發(fā)運行,設置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在并行的方式下運行。
我們已經(jīng)創(chuàng )建了job details,jobs。我們回顧了允許你調用特定對象上某一個(gè)方法的便捷的bean。 當然我們仍需要調度這些jobs。這需要使用triggers和SchedulerFactoryBean來(lái)完成。 Quartz自帶一些可供使用的triggers。Spring提供兩個(gè)子類(lèi)triggers,分別為CronTriggerBean和SimpleTriggerBean。
Triggers也需要被調度。Spring提供SchedulerFactoryBean來(lái)暴露一些屬性來(lái)設置triggers。SchedulerFactoryBean負責調度那些實(shí)際的triggers。
兩個(gè)例子:
<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>
現在我們創(chuàng )建了兩個(gè)triggers,其中一個(gè)開(kāi)始延遲10秒以后每50秒運行一次,另一個(gè)每天早上6點(diǎn)鐘運行。 我們需要創(chuàng )建一個(gè)SchedulerFactoryBean來(lái)最終實(shí)現上述的一切:
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref local="cronTrigger"/><ref local="simpleTrigger"/></list></property></bean>
更多的一些屬性你可以通過(guò)SchedulerFactoryBean來(lái)設置,例如job details使用的Calendars,用來(lái)訂制Quartz的一些屬性以及其它。 你可以看相應的JavaDOC(http://www.springframework.org/docs/api/org/springframework/scheduling/quartz/SchedulerFactoryBean.html)來(lái)了解進(jìn)一步的信息。
另外一個(gè)調度任務(wù)的途徑是使用JDK Timer對象。更多的關(guān)于Timers的信息可以在這里http://java.sun.com/docs/books/tutorial/essential/threads/timer.html找到。 上面討論的概念仍可以應用于Timer的支持。你可以創(chuàng )建定制的timer或者調用某些方法的timer。 包裝timers的工作由TimerFactoryBean完成。
你可以使用TimerTask創(chuàng )建定制的timer tasks,類(lèi)似于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}}包裝它是簡(jiǎn)單的:
<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>
就像Quartz的支持一樣,Timer的支持也有一個(gè)組件允許你周期性地調用一個(gè)方法:
<bean id="methodInvokingTask"class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean"><property name="targetObject"><ref bean="exampleBusinessObject"/></property><property name="targetMethod"><value>doIt</value></property></bean>
上面的例子將會(huì )導致exampleBusinessObject上的doIt方法被調用(如下):
public class BusinessObject {// properties and collaboratorspublic void doIt() {// do the actual work}}把上面例子中提到ScheduledTimerTask的引用改為methodInvokingTask將導致該task被執行。
TimerFactoryBean類(lèi)似于QuartzSchedulerFactoryBean,都是服務(wù)于一個(gè)目的:建立起實(shí)際的時(shí)序調度。 TimerFactoryBean建立一個(gè)實(shí)際的Timer來(lái)調度它引用的那些tasks。你可以指定它是否使用一個(gè)守護線(xiàn)程。
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean"><property name="scheduledTimerTasks"><list><!-- see the example above --><ref local="scheduledTask"/></list></property></bean>
就是這些了!
聯(lián)系客服