JAVA中 Spring定時器的兩種實現方式_java

來源:互聯網
上載者:User

目前有兩種流行Spring定時器配置:Java的Timer類和OpenSymphony的Quartz。

1.Java Timer定時

首先繼承java.util.TimerTask類實現run方法

import java.util.TimerTask;  public class EmailReportTask extends TimerTask{    @Override    public void run() {      ...    }   }

在Spring定義

...

配置Spring定時器

<bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">  <property name="timerTask" ref="reportTimerTask" />  <property name="period">  <value>86400000value>  property>  bean> 

timerTask屬性告訴ScheduledTimerTask運行哪個。86400000代表24個小時

啟動Spring定時器

Spring的TimerFactoryBean負責啟動定時任務

<bean class="org.springframework.scheduling.timer.TimerFactoryBean">  <property name="scheduledTimerTasks">    <list><ref bean="scheduleReportTask"/>list>  property>  bean> 

scheduledTimerTasks裡顯示一個需要啟動的定時器任務的列表。

可以通過設定delay屬性延遲啟動  

<bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">  <property name="timerTask" ref="reportTimerTask" />  <property name="period">  <value>86400000value>  property>  <property name="delay">  <value>3600000value>  property>  bean>  

這個任務我們只能規定每隔24小時運行一次,無法精確到某時啟動

2.Quartz定時器

首先繼承QuartzJobBean類實現executeInternal方法

import org.quartz.JobExecutionContext;  import org.quartz.JobExecutionException;  import org.springframework.scheduling.quartz.QuartzJobBean;  public class EmailReportJob extends QuartzJobBean{  protected void executeInternal(JobExecutionContext arg0)  throws JobExecutionException {  ...  }  }

在Spring中定義

<bean id="reportJob" class="org.springframework.scheduling.quartz.JobDetailBean">  <property name="jobClass">  <value>EmailReportJobvalue>  property>  <property name="jobDataAsMap">    <map>      <entry key="courseService">        <ref bean="courseService"/>        entry>    map>  property>  bean> 

在這裡我們並沒有直接聲明一個EmailReportJob Bean,而是聲明了一個JobDetailBean。這個是Quartz的特點。JobDetailBean是Quartz的org.quartz.JobDetail的子類,它要求通過jobClass屬性來設定一個Job對象。

使用Quartz的JobDetail中的另一個特別之處是EmailReportJob的courseService屬性是間接設定的。JobDetail的jobDataAsMap屬性接受一個Map,包括設定給jobClass的各種屬性,當。JobDetailBean執行個體化時,它會將courseService Bean注入到EmailReportJob 的courseService 屬性中。

啟動定時器

Quartz的org.quartz.Trigger類描述了何時及以怎樣的頻度運行一個Quartz工作。Spring提供了兩個觸發器SimpleTriggerBean和CronTriggerBean。

SimpleTriggerBean與scheduledTimerTasks類似。指定工作的執行頻度,模仿scheduledTimerTasks配置 .

<bean id="simpleReportTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">  <property name="jobDetail" ref="reprotJob" />  <property name="startDelay">  <value>360000value>  property>  <property name="repeatInterval">    <value>86400000value>  property>  bean>

startDelay也是延遲1個小時啟動

CronTriggerBean指定工作的準確已耗用時間

<bean id="cronReportTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  <property name="jobDetail" ref="reprotJob" />  <property name="cronExpression">  <value>0 0 6 * * ?value>  property>  bean> 

屬性cronExpression告訴何時觸發。最神秘就是cron運算式:

Linux系統的計劃任務通常有cron來承擔。一個cron運算式有至少6個(也可能7個)有空格分隔的時間元素。從左至右:

1.秒2.分3.小時4.月份中的日期(1-31)5.月份(1-12或JAN-DEC)6.星期中的日期(1-7或SUN-SAT)7.年份(1970-2099)
每個元素都顯示的規定一個值(如6),一個區間(9-12),一個列表(9,11,13)或一個萬用字元(*)。因為4和6這兩個元素是互斥的,因此應該通過設定一個問號(?)來表明不想設定的那個欄位,“/”如果值組合就表示重複次數(10/6表示每10秒重複6次)。

啟動定時器

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

triggers屬性接受一組觸發器。

好了,本文內容到此結束了,寫的還不錯吧,有不足之處,歡迎各位大俠提出寶貴意見。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.