Spring 中使用Quartz實現(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{
//這個參數(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表達式,可以指定時間執(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)度工廠實例化后,經(jīng)過10秒開始執(zhí)行調(diào)度--> <property name="repeatInterval" value="2000" /><!--每2秒調(diào)度一次--> </bean>
第二種 CronTriggerBean,比如每天12點執(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表達式格式最后面介紹。
第四步配置調(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點執(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表達式
Cron表達式是一個字符串,字符串以5或6個空格隔開,分為6或7個域,每一個域代表一個含義,Cron有如下兩種語法格式:
Seconds Minutes Hours DayofMonth Month DayofWeek Year//或 Seconds Minutes Hours DayofMonth Month DayofWeek
每一個域可出現(xiàn)的字符如下:
- Seconds:可出現(xiàn)", - * /"四個字符,有效范圍為0-59的整數(shù)
- Minutes:可出現(xiàn)", - * /"四個字符,有效范圍為0-59的整數(shù)
- Hours:可出現(xiàn)", - * /"四個字符,有效范圍為0-23的整數(shù)
- DayofMonth:可出現(xiàn)", - * / ? L W C"八個字符,有效范圍為0-31的整數(shù)
- Month:可出現(xiàn)", - * /"四個字符,有效范圍為1-12的整數(shù)或JAN-DEc
- DayofWeek:可出現(xiàn)", - * / ? L C #"四個字符,有效范圍為1-7的整數(shù)或SUN-SAT兩個范圍。1表示星期天,2表示星期一, 依次類推
- Year:可出現(xiàn)", - * /"四個字符,有效范圍為1970-2099年
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Web MVC和Hibernate的集成配置詳解
這篇文章主要介紹了Spring Web MVC和Hibernate的集成配置詳解,具有一定借鑒價值,需要的朋友可以參考下2017-12-12
Java并發(fā)編程之LongAdder執(zhí)行情況解析
這篇文章主要為大家介紹了Java并發(fā)編程之LongAdder執(zhí)行情況解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
Java新特性之Nashorn_動力節(jié)點Java學院整理
這篇文章主要介紹了Java新特性之Nashorn的相關(guān)資料,需要的朋友可以參考下2017-06-06

