前言:Spring中使用Quartz 有兩種方式,一種是繼承特定的基類:org.springframework.scheduling.quartz.QuartzJobBean,另一種則不需要,(推薦使用第二種)。下面分別介紹。
1、作業類繼承 org.springframework.scheduling.quartz.QuartzJobBean
第一步:定義作業類
java代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class Job1 extends QuartzJobBean{ //這個參數值由xml配置傳過來 private int timeout; public void setTimeout( int timeout) { this .timeout = timeout; } @Override protected void executeInternal(JobExecutionContext content) throws JobExecutionException { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); System.out.println(sdf.format( new Date()) + "job1執行" + "這是xml里給timeout賦值" + timeout); } } |
第二步spring中配置JobDetailBean
spring.xml配置代碼
1
2
3
4
5
6
7
8
9
10
|
< bean id = "job1" class = "org.springframework.scheduling.quartz.JobDetailBean" > <!-- 這里指向寫好的作業類 --> < property name = "jobClass" value = "com.ccg.job.Job1" /> < property name = "jobDataAsMap" > < map > <!-- 這里寫參數可以傳到作業類中定義的參數 --> < entry key = "timeout" value = "10" ></ entry > </ map > </ property > </ bean > |
第三步配置觸發方式
Quartz的作業觸發器有兩種,分別是
org.springframework.scheduling.quartz.SimpleTriggerBean ,按照一定頻率執行任務
org.springframework.scheduling.quartz.CronTriggerBean ,支持cron表達式,可以指定時間執行,也可以按照頻率執行
第一種 SimpleTriggerBean,比如每兩秒執行一次,xml配置如下:
1
2
3
4
5
|
< bean id = "simpleTrigger" class = "org.springframework.scheduling.quartz.SimpleTriggerBean" > < property name = "jobDetail" ref = "job1" /> < property name = "startDelay" value = "10000" /> <!--調度工廠實例化后,經過10秒開始執行調度--> < property name = "repeatInterval" value = "2000" /> <!--每2秒調度一次--> </ bean > |
第二種 CronTriggerBean,比如每天12點執行,xml配置如下:
1
2
3
4
|
< bean id = "cronTrigger" class = "org.springframework.scheduling.quartz.CronTriggerBean" > < property name = "jobDetail" ref = "job1" /> < property name = "cronExpression" value = "0 0 12 * * ?" /> <!-- 每天12天執行任務 --> </ bean > |
Cron表達式格式最后面介紹。
第四步配置調度工廠
spring.xml配置代碼如下:
1
2
3
4
5
6
7
8
|
< bean class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" > < property name = "triggers" > < list > < ref bean = "simpleTrigger" /> <!-- <ref bean="cronTrigger"/> --> </ list > </ property > </ bean > |
第五步啟動應用,查看任務調度執行情況。
2、作業類不需要繼承,只是普通的java類
主要的類是org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ,下面是代碼:
第一步作業類
1
2
3
4
5
6
7
8
9
10
|
import java.text.SimpleDateFormat; import java.util.Date; public class Job2 { public void run(){ SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); System.out.println(sdf.format( new Date()) + "這里是job2的執行" ); } } |
第二步在spring.xml中配置job2
1
2
3
4
5
6
7
|
< bean id = "job2" class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > < property name = "targetObject" > < bean class = "com.ccg.job.Job2" /> </ property > < property name = "targetMethod" value = "run" ></ property > < property name = "concurrent" value = "false" /> <!-- 作業不并發調度 --> </ bean > |
targetObject 執行作業類 targetMethod指向作業類中要執行的方法
第三步配置觸發方式,同樣是有兩種一種SimpleTrggerBean,一種CronTrggerBean
第一種配置xml如下:(每2秒執行一次)
1
2
3
4
5
|
< bean id = "simpleTrigger" class = "org.springframework.scheduling.quartz.SimpleTriggerBean" > < property name = "jobDetail" ref = "job2" /> < property name = "startDelay" value = "10000" /> < property name = "repeatInterval" value = "2000" /> </ bean > |
第二種配置xml如下:(每天12點執行)
1
2
3
4
|
< bean id = "cronTrigger" class = "org.springframework.scheduling.quartz.CronTriggerBean" > < property name = "jobDetail" ref = "job2" /> < property name = "cronExpression" value = "0 0 12 * * ?" /> </ bean > |
第四步配置調度工廠
1
2
3
4
5
6
7
|
< bean class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" > < property name = "triggers" > < list > < ref bean = "simpleTrigger" /> </ list > </ property > </ bean > |
如果使用CronTriggerBean 需要把simpleTrigger 換成simpleTrigger
最后啟動服務,查看任務調度執行情況。
附:Cron表達式
Cron表達式是一個字符串,字符串以5或6個空格隔開,分為6或7個域,每一個域代表一個含義,Cron有如下兩種語法格式:
1
2
|
Seconds Minutes Hours DayofMonth Month DayofWeek Year //或 Seconds Minutes Hours DayofMonth Month DayofWeek |
每一個域可出現的字符如下:
- Seconds:可出現", - * /"四個字符,有效范圍為0-59的整數
- Minutes:可出現", - * /"四個字符,有效范圍為0-59的整數
- Hours:可出現", - * /"四個字符,有效范圍為0-23的整數
- DayofMonth:可出現", - * / ? L W C"八個字符,有效范圍為0-31的整數
- Month:可出現", - * /"四個字符,有效范圍為1-12的整數或JAN-DEc
- DayofWeek:可出現", - * / ? L C #"四個字符,有效范圍為1-7的整數或SUN-SAT兩個范圍。1表示星期天,2表示星期一, 依次類推
- Year:可出現", - * /"四個字符,有效范圍為1970-2099年
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/FlyHeLanMan/p/6401776.html