小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

第18章使用Quartz或Timer完成時序調(diào)度工作

 昵稱h9yp3 2007-07-03

第 18 章 使用Quartz或Timer完成時序調(diào)度工作

18.1. 簡介

Spring提供了支持時序調(diào)度(譯者注:Scheduling,下同)的整合類.現(xiàn)在, Spring支持內(nèi)置于1.3版本以來的JDK中的Timer和Quartz Scheduler(http://www.)。 兩個時序調(diào)度器通過FactoryBean建立,保持著可選的對Timers或者Triggers的引用。更進一步的, 對于Quartz Scheduler和Timer兩者存在一個方便的類允許你調(diào)用目標對象(類似于通常的MethodInvokingFactoryBeans)上的某個方法

18.2. 使用OpenSymphony Quartz Scheduler

Quartz使用Triggers,JobsJobDetail來實現(xiàn)時序調(diào)度中的各種工作。 為了了解Quartz背后的種種基本觀點,你可以移步至http://www./quartz。 為了方便的使用,Spring提供了幾個類在基于Spring的應(yīng)用中來簡化對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中的一些其他的設(shè)定對你來說也是可以同樣設(shè)置的.

注意:使用namegroup屬性,你可以修改job在哪一個組下運行和使用什么名稱。 默認情況下,job的名稱等于job detai bean的名稱(在上面的例子中為exampleJob)。

18.2.2. 使用MethodInvokingJobDetailFactoryBean

通常情況下,你只需要調(diào)用特定對象上的一個方法。你可以使用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>

上面例子將導(dǎo)致exampleBusinessObject中的doIt方法被調(diào)用(如下):

public class BusinessObject {
// properties and collaborators
public void doIt() {
// do the actual work
}
}

 

<bean id="exampleBusinessObject" class="examples.ExampleBusinessObject"/>

使用MethodInvokingJobDetailFactoryBean你不需要創(chuàng)建只有一行代碼且只調(diào)用一個方法的job, 你只需要創(chuàng)建真實的業(yè)務(wù)對象來包裝具體的細節(jié)的對象。

默認情況下,Quartz Jobs是無狀態(tài)的,可能導(dǎo)致jobs之間互相的影響。如果你為相同的JobDetail指定兩個觸發(fā)器, 很可能當(dāng)?shù)谝粋€job完成之前,第二個job就開始了。如果JobDetail對象實現(xiàn)了Stateful接口,就不會發(fā)生這樣的事情。 第二個job將不會在第一個job完成之前開始。為了使得jobs不并發(fā)運行,設(shè)置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來包裝任務(wù)

我們已經(jīng)創(chuàng)建了job details,jobs。我們回顧了允許你調(diào)用特定對象上某一個方法的便捷的bean。 當(dāng)然我們?nèi)孕枰{(diào)度這些jobs。這需要使用triggers和SchedulerFactoryBean來完成。 Quartz自帶一些可供使用的triggers。Spring提供兩個子類triggers,分別為CronTriggerBeanSimpleTriggerBean

Triggers也需要被調(diào)度。Spring提供SchedulerFactoryBean來暴露一些屬性來設(shè)置triggers。SchedulerFactoryBean負責(zé)調(diào)度那些實際的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>

現(xiàn)在我們創(chuàng)建了兩個triggers,其中一個開始延遲10秒以后每50秒運行一次,另一個每天早上6點鐘運行。 我們需要創(chuàng)建一個SchedulerFactoryBean來最終實現(xiàn)上述的一切:

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="cronTrigger"/>
<ref local="simpleTrigger"/>
</list>
</property>
</bean>

更多的一些屬性你可以通過SchedulerFactoryBean來設(shè)置,例如job details使用的Calendars,用來訂制Quartz的一些屬性以及其它。 你可以看相應(yīng)的JavaDOC(http://www./docs/api/org/springframework/scheduling/quartz/SchedulerFactoryBean.html)來了解進一步的信息。

18.3. 使用JDK Timer支持類

另外一個調(diào)度任務(wù)的途徑是使用JDK Timer對象。更多的關(guān)于Timers的信息可以在這里http://java./docs/books/tutorial/essential/threads/timer.html找到。 上面討論的概念仍可以應(yīng)用于Timer的支持。你可以創(chuàng)建定制的timer或者調(diào)用某些方法的timer。 包裝timers的工作由TimerFactoryBean完成。

18.3.1. 創(chuàng)建定制的timers

你可以使用TimerTask創(chuàng)建定制的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@</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的支持也有一個組件允許你周期性地調(diào)用一個方法:

<bean id="methodInvokingTask"
class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
<property name="targetObject"><ref bean="exampleBusinessObject"/></property>
<property name="targetMethod"><value>doIt</value></property>
</bean>

上面的例子將會導(dǎo)致exampleBusinessObject上的doIt方法被調(diào)用(如下):

public class BusinessObject {
// properties and collaborators
public void doIt() {
// do the actual work
}
}

把上面例子中提到ScheduledTimerTask的引用改為methodInvokingTask將導(dǎo)致該task被執(zhí)行。

18.3.3. 包裝:使用TimerFactoryBean來建立tasks

TimerFactoryBean類似于QuartzSchedulerFactoryBean,都是服務(wù)于一個目的:建立起實際的時序調(diào)度。 TimerFactoryBean建立一個實際的Timer來調(diào)度它引用的那些tasks。你可以指定它是否使用一個守護線程。

<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<!-- see the example above -->
<ref local="scheduledTask"/>
</list>
</property>
</bean>

就是這些了!

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多