您好,登錄后才能下訂單哦!
前言: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{ //這個參數(shù)值由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執(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á)式,可以指定時間執(zhí)行,也可以按照頻率執(zhí)行
第一種 SimpleTriggerBean,比如每兩秒執(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)過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>
第五步啟動應(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
最后啟動服務(wù),查看任務(wù)調(diào)度執(zhí)行情況。
附:Cron表達(dá)式
Cron表達(dá)式是一個字符串,字符串以5或6個空格隔開,分為6或7個域,每一個域代表一個含義,Cron有如下兩種語法格式:
Seconds Minutes Hours DayofMonth Month DayofWeek Year//或 Seconds Minutes Hours DayofMonth Month DayofWeek
每一個域可出現(xiàn)的字符如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。