Spring 中使用Quartz實(shí)現(xiàn)任務(wù)調(diào)度
前言:Spring中使用Quartz 有兩種方式,一種是繼承特定的基類:org.springframework.scheduling.quartz.QuartzJobBean,另一種則不需要,(推薦使用第二種)。下面分別介紹。
1、作業(yè)類繼承 org.springframework.scheduling.quartz.QuartzJobBean
第一步:定義作業(yè)類
java代碼
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{ //這個(gè)參數(shù)值由xml配置傳過(guò)來(lái) 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執(zhí)行" + "這是xml里給timeout賦值" + timeout); } }
第二步spring中配置JobDetailBean
spring.xml配置代碼
<bean id = "job1" class="org.springframework.scheduling.quartz.JobDetailBean"> <!-- 這里指向?qū)懞玫淖鳂I(yè)類 --> <property name="jobClass" value="com.ccg.job.Job1" /> <property name="jobDataAsMap"> <map> <!-- 這里寫參數(shù)可以傳到作業(yè)類中定義的參數(shù) --> <entry key="timeout" value="10"></entry> </map> </property> </bean>
第三步配置觸發(fā)方式
Quartz的作業(yè)觸發(fā)器有兩種,分別是
org.springframework.scheduling.quartz.SimpleTriggerBean ,按照一定頻率執(zhí)行任務(wù)
org.springframework.scheduling.quartz.CronTriggerBean ,支持cron表達(dá)式,可以指定時(shí)間執(zhí)行,也可以按照頻率執(zhí)行
第一種 SimpleTriggerBean,比如每?jī)擅雸?zhí)行一次,xml配置如下:
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="job1" /> <property name="startDelay" value="10000" /><!--調(diào)度工廠實(shí)例化后,經(jīng)過(guò)10秒開始執(zhí)行調(diào)度--> <property name="repeatInterval" value="2000" /><!--每2秒調(diào)度一次--> </bean>
第二種 CronTriggerBean,比如每天12點(diǎn)執(zhí)行,xml配置如下:
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="job1" /> <property name="cronExpression" value="0 0 12 * * ?" /> <!-- 每天12天執(zhí)行任務(wù) --> </bean>
Cron表達(dá)式格式最后面介紹。
第四步配置調(diào)度工廠
spring.xml配置代碼如下:
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="simpleTrigger"/> <!-- <ref bean="cronTrigger"/> --> </list> </property> </bean>
第五步啟動(dòng)應(yīng)用,查看任務(wù)調(diào)度執(zhí)行情況。
2、作業(yè)類不需要繼承,只是普通的java類
主要的類是org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ,下面是代碼:
第一步作業(yè)類
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的執(zhí)行"); } }
第二步在spring.xml中配置job2
<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" /><!-- 作業(yè)不并發(fā)調(diào)度 --> </bean>
targetObject 執(zhí)行作業(yè)類 targetMethod指向作業(yè)類中要執(zhí)行的方法
第三步配置觸發(fā)方式,同樣是有兩種一種SimpleTrggerBean,一種CronTrggerBean
第一種配置xml如下:(每2秒執(zhí)行一次)
<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點(diǎn)執(zhí)行)
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="job2" /> <property name="cronExpression" value="0 0 12 * * ?" /> </bean>
第四步配置調(diào)度工廠
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="simpleTrigger"/> </list> </property> </bean>
如果使用CronTriggerBean 需要把simpleTrigger 換成simpleTrigger
最后啟動(dòng)服務(wù),查看任務(wù)調(diào)度執(zhí)行情況。
附:Cron表達(dá)式
Cron表達(dá)式是一個(gè)字符串,字符串以5或6個(gè)空格隔開,分為6或7個(gè)域,每一個(gè)域代表一個(gè)含義,Cron有如下兩種語(yǔ)法格式:
Seconds Minutes Hours DayofMonth Month DayofWeek Year//或 Seconds Minutes Hours DayofMonth Month DayofWeek
每一個(gè)域可出現(xiàn)的字符如下:
- Seconds:可出現(xiàn)", - * /"四個(gè)字符,有效范圍為0-59的整數(shù)
- Minutes:可出現(xiàn)", - * /"四個(gè)字符,有效范圍為0-59的整數(shù)
- Hours:可出現(xiàn)", - * /"四個(gè)字符,有效范圍為0-23的整數(shù)
- DayofMonth:可出現(xiàn)", - * / ? L W C"八個(gè)字符,有效范圍為0-31的整數(shù)
- Month:可出現(xiàn)", - * /"四個(gè)字符,有效范圍為1-12的整數(shù)或JAN-DEc
- DayofWeek:可出現(xiàn)", - * / ? L C #"四個(gè)字符,有效范圍為1-7的整數(shù)或SUN-SAT兩個(gè)范圍。1表示星期天,2表示星期一, 依次類推
- Year:可出現(xiàn)", - * /"四個(gè)字符,有效范圍為1970-2099年
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java接口自動(dòng)化測(cè)試框架設(shè)計(jì)之Get請(qǐng)求方法和測(cè)試詳解
這篇文章主要介紹了Java接口自動(dòng)化測(cè)試框架設(shè)計(jì) Get請(qǐng)求方法和測(cè)試,框架設(shè)計(jì)我們只是介紹基本的組件,而且框架設(shè)計(jì)沒(méi)有想象那么難,一步一步跟著做就會(huì)了。這篇我們來(lái)演示,如果通過(guò)Java代碼來(lái)實(shí)現(xiàn)一個(gè)用純代碼實(shí)現(xiàn)Http中的Get請(qǐng)求過(guò)程,需要的朋友可以參考下2019-07-07mysql數(shù)據(jù)庫(kù)忘記密碼時(shí)如何修改
本文主要介紹了mysql數(shù)據(jù)庫(kù)忘記密碼時(shí)如何修改的步驟方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02基于Springboot實(shí)現(xiàn)JWT認(rèn)證的示例代碼
本文主要介紹了基于Springboot實(shí)現(xiàn)JWT認(rèn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11如何用Java將數(shù)據(jù)庫(kù)的數(shù)據(jù)生成pdf返回給前端用戶下載
本文詳細(xì)介紹了使用SpringBoot、iText庫(kù)、MyBatis等技術(shù)從數(shù)據(jù)庫(kù)中選取數(shù)據(jù)并生成PDF文件的后端處理流程,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09Java 多線程學(xué)習(xí)詳細(xì)總結(jié)
本文主要介紹 Java 多線程的知識(shí)資料,這里整理了詳細(xì)的多線程內(nèi)容,及簡(jiǎn)單實(shí)現(xiàn)代碼,有需要的朋友可以參考下2016-09-09Spring配置多數(shù)據(jù)源導(dǎo)致事物無(wú)法回滾問(wèn)題
這篇文章主要介紹了Spring配置多數(shù)據(jù)源導(dǎo)致事物無(wú)法回滾問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01淺談SpringBoot如何封裝統(tǒng)一響應(yīng)體
今天帶各位小伙伴學(xué)習(xí)SpringBoot如何封裝統(tǒng)一響應(yīng)體,文中有非常詳細(xì)的介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05