目前有兩種流行Spring定時器配置:Java的Timer類和OpenSymphony的Quartz。
1.Java Timer定時
首先繼承java.util.TimerTask類實現run方法
1
2
3
4
5
6
7
|
import java.util.TimerTask; public class EmailReportTask extends TimerTask{ @Override public void run() { ... } } |
在Spring定義
...
配置Spring定時器
1
2
3
4
5
6
|
<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負責啟動定時任務
1
2
3
4
5
|
<bean class = "org.springframework.scheduling.timer.TimerFactoryBean" > <property name= "scheduledTimerTasks" > <list><ref bean= "scheduleReportTask" />list> property> bean> |
scheduledTimerTasks里顯示一個需要啟動的定時器任務的列表。
可以通過設置delay屬性延遲啟動
1
2
3
4
5
6
7
8
9
|
<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方法
1
2
3
4
5
6
7
8
9
|
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中定義
1
2
3
4
5
6
7
8
9
10
11
12
|
<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配置 .
1
2
3
4
5
6
7
8
9
|
<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指定工作的準確運行時間
1
2
3
4
5
6
|
<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次)。
啟動定時器
1
2
3
4
5
|
<bean class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" > <property name= "triggers" > <list><ref bean= "cronReportTrigger" />list> property> bean> |
triggers屬性接受一組觸發器。
好了,本文內容到此結束了,寫的還不錯吧,有不足之處,歡迎各位大俠提出寶貴意見。