Spring結合Quartz進行任務調度

來源:互聯網
上載者:User
Job和JobDetail類定義 使用JobDetailFactoryBean

XML設定檔

<bean name="exampleJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">    <property name="jobClass" value="com.wilson.demo.job.ExampleJob"></property>    <property name="jobDataAsMap">        <map>            <entry key="timeOut" value="5"></entry>        </map>    </property></bean>

jobClass:制定任務對應的類,該類必須繼承QuartzJobBean抽象類別;
jobDataAsMap:資料承載類,該Map中的屬性可注入到jobClass中。

ExampleJob.java

public class ExampleJob extends QuartzJobBean{    private int timeOut;    /**     * @param timeOut the timeOut to set     */    public void setTimeOut(int timeOut) {        this.timeOut = timeOut;    }    @Override    protected void executeInternal(JobExecutionContext context)            throws JobExecutionException {        System.out.println("ExampleJob>>>>TimeOut value:"+timeOut+",Job running time:"+context.getFireTime());    }}
使用MethodInvokingJobDetailFactoryBean

XML檔案

<bean name="pojoObject" class="com.wilson.demo.job.PoJoExampleJob"></bean><bean name="methodJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">    <property name="targetObject" ref="pojoObject"></property>    <property name="targetMethod" value="runForJob"></property>    <property name="concurrent" value="false"></property></bean>

pojoObject:任務執行類,普通POJO類,不用實現任何介面。需要注意的時,如果該Bean的生命週期是Singleton,則每次任務執行時,都是同一個Bean執行個體。
targetObject:制定任務的執行類;
targetMethod:制定執行任務時調用的方法;
concurrent:前一個任務未結束時,下一次調用能否開始。 兩種方式的比較 JobDetailFactoryBean 必須繼承QuartzJobBean抽象類別; 支援往任務執行類中注入屬性,任務執行類能擷取執行時的上下文環境; 通過該工廠Bean建立的JobDetail對象能夠序列化,支援持久性的任務,同時,每一次任務調用時均產生新的任務類執行個體; 每次任務調用時,共用同一個JobDetail執行個體。 MethodInvokingJobDetailFactoryBean 支援普通POJO對象,無需繼承QuartzJobBean; 產生的JobDetail bean無法持久化,不支援持久性的任務; 如果targetObject引用的Bean是Singleton,則每次任務調用時,均使用相同的執行個體bean; 每次任務調用時,共用同一個JobDetail執行個體。 定義Trigger和Scheduler

XML配置

<!-- 簡單的Trigger配置,啟動延時為10秒,每5秒執行一次任務 --><bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">    <property name="jobDetail" ref="methodJobDetail"></property>    <property name="startDelay" value="10000"></property>    <property name="repeatInterval" value="500000000"></property></bean><!-- 支援運算式的Trigger,每周五,每分鐘的第5秒開始,每7秒執行一次,每分鐘執行7次 --><bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">    <property name="jobDetail" ref="exampleJobDetail"></property>    <property name="cronExpression" value="5/7 * * ? * 6"></property></bean><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">    <property name="triggers">        <list>            <ref bean="cronTrigger"/>            <ref bean="simpleTrigger"/>        </list>    </property></bean>

Scheduler是具體的任務調度器,而Trigger作為觸發器會關聯具體的JobDetail對象,通過JobDetail對象知道具體的任務執行類。
CronTrigger的運算式規則:(摘自官方文檔-傳送門)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.