Spring中實(shí)現(xiàn)定時(shí)調(diào)度的幾種方法
1,內(nèi)容簡(jiǎn)介
所謂的定時(shí)調(diào)度,是指在無(wú)人值守的時(shí)候系統(tǒng)可以在某一時(shí)刻執(zhí)行某些特定的功能采用的一種機(jī)制,對(duì)于傳統(tǒng)的開(kāi)發(fā)而言,定時(shí)調(diào)度的操作分為兩種形式:
定時(shí)觸發(fā):到某一時(shí)間點(diǎn)上執(zhí)行某些處理操作;
間隔觸發(fā):每隔幾秒后進(jìn)行某些操作的自動(dòng)處理。
所有的處理都依賴于計(jì)算機(jī)系統(tǒng)底層的時(shí)鐘發(fā)生器,在java最初的實(shí)現(xiàn)過(guò)程里面,真對(duì)于定時(shí)處理專門(mén)提供有兩個(gè)類:Timer,TimerTask兩個(gè)類,其中TimerTask主要是定義任務(wù)的執(zhí)行,相當(dāng)于啟動(dòng)一個(gè)線程去執(zhí)行某些任務(wù)。
public class MyTask extends TimerTask{
@Override
public void run() {//定義要執(zhí)行的任務(wù)
// TODO Auto-generated method stub
String currentTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
System.out.println(currentTime);
}
}
public class MyTaskTest {
public static void main(String[] args) {
Timer timer=new Timer();
timer.schedule(new MyTask(), 1000);//啟動(dòng)任務(wù),延遲1秒后執(zhí)行。
}
}
但是,如果要求要在每年的某月某時(shí)某分某秒執(zhí)行某個(gè)任務(wù),使用Timer和TimerTask就無(wú)能為力了。在項(xiàng)目開(kāi)發(fā)中往往會(huì)有兩種定時(shí)控制的選擇:
quartz組件:企業(yè)及定時(shí)調(diào)度組件,需要單獨(dú)配置;
SpringTask:輕量級(jí)組件,配置簡(jiǎn)單,可以利用Annotation實(shí)現(xiàn)配置處理。
2,Quartz定義定時(shí)調(diào)度
使用Quartz組件,我們需要導(dǎo)入quartz的開(kāi)發(fā)包,在pom.xml中添加quartz的開(kāi)發(fā)包。
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
引入包后,就可以進(jìn)行定時(shí)調(diào)度的開(kāi)發(fā)了。
有兩種實(shí)現(xiàn)模式:
去繼承QuartzJobBean父類;
直接利用配置就可以實(shí)現(xiàn)方法的調(diào)度控制。
1,繼承一個(gè)父類實(shí)現(xiàn)任務(wù)的處理。
public class MyTask2 extends QuartzJobBean{
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
// TODO Auto-generated method stub
String currentTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
System.out.println(currentTime);
System.out.println("具體的任務(wù)實(shí)現(xiàn)?。?!");
}
}
所有的定時(shí)調(diào)度的啟用都要在Spring的控制文件中完成,即,不需要去寫(xiě)一個(gè)明確的類來(lái)進(jìn)行定時(shí)任務(wù)啟用。
2,在applicationContext.xml文件中增加定時(shí)調(diào)度的配置,通過(guò)定時(shí)調(diào)度工廠類實(shí)現(xiàn)。
<bean id="taskFactory"
class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="cn.wnh.timerSask.MyTask1" />
<property name="jobDataMap">
<map>
<entry key="timeout" value="0" />
</map>
</property>
</bean>
隨后配置任務(wù)的觸發(fā)作業(yè),對(duì)于作業(yè)的配置有兩類:
使用間隔觸發(fā):若干時(shí)間之后重復(fù)執(zhí)行;
工廠類:org.springframework.scheduling.quartz.SimpleTriggerFactoryBean
<bean id="simpleTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<!-- 定義間隔觸發(fā)的執(zhí)行程序類 -->
<property name="jobDetail" ref="taskFactory"></property>
<!-- 設(shè)置定時(shí)觸發(fā)延遲時(shí)間 -->
<property name="startDelay" value="0"></property>
<!-- 單位是”毫秒“ -->
<property name="repeatInterval" value="2000"></property>
</bean>
設(shè)置間隔觸發(fā)調(diào)度器:org.springframework.scheduling.quartz.SchedulerFactoryBean
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>
3,此時(shí)所有的間隔觸發(fā)控制都交由Spring管理了,現(xiàn)在只需要啟動(dòng)Spring容器即可實(shí)現(xiàn)間隔觸發(fā)任務(wù)。
使用Cron實(shí)現(xiàn)定時(shí)觸發(fā)
Quartz不僅可以實(shí)現(xiàn)間隔觸發(fā),它還可以結(jié)合Cron實(shí)現(xiàn)定時(shí)觸發(fā),這也是它最重要的功能。
一般項(xiàng)目中使用最多的模式:小時(shí)觸發(fā),月初觸發(fā),年初觸發(fā)。
修改之前的間隔觸發(fā)配置,使用CronTriggerFactoryBean實(shí)現(xiàn)定時(shí)觸發(fā)。
<bean id="taskFactory"
class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="cn.wnh.timerSask.MyTask1" />
<property name="jobDataMap">
<map>
<entry key="timeout" value="0" />
</map>
</property>
</bean>
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="taskFactory" />
<!-- cron表達(dá)式,描述每分鐘觸發(fā)一次 -->
<property name="cronExpression" value="0 * * * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
啟動(dòng)Spring容器即可實(shí)現(xiàn)。
2,不繼承任何類實(shí)現(xiàn)定時(shí)調(diào)度
在項(xiàng)目開(kāi)發(fā)中,繼承直接會(huì)導(dǎo)致單繼承的局限控制,所以在這種情況下Spring中提供了一種可以不繼承任何類即可實(shí)現(xiàn)定時(shí)操作的任務(wù)處理。
定義一個(gè)任務(wù)執(zhí)行類,不繼承任何類。
public class MyTask2 {
public void taskSelf(){
String task=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new java.util.Date());
System.out.println(task);
System.out.println("執(zhí)行具體任務(wù)操作");
}
}
在applicationContext.xml在配置工廠類:org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean
<bean id="taskFactory2"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="cn.wnh.timerSask.MyTask2" />
</property>
<!--配置要執(zhí)行的方法 -->
<property name="targetMethod" value="taskSelf" />
</bean>
隨后在任務(wù)調(diào)度配置上配置新的程序類
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="taskFactory2" />
<!-- cron表達(dá)式,描述每分鐘觸發(fā)一次 -->
<property name="cronExpression" value="* * * * * ?" />
</bean>
啟動(dòng)容器即可實(shí)現(xiàn)定時(shí)調(diào)度。
這種模式?jīng)]有類的繼承依賴,處理會(huì)更加靈活。
Spring Task實(shí)現(xiàn)定時(shí)調(diào)度
在Spring中自己有對(duì)定時(shí)調(diào)度的支持,使用起來(lái)感覺(jué)比Quartz還要好用。
它有兩種實(shí)現(xiàn)方式,1,在applicationContext.xml中配置實(shí)現(xiàn);2,使用Annotation實(shí)現(xiàn)。
不過(guò)使用什么模式,必須先有一個(gè)任務(wù)處理類。
定義任務(wù)處理類。
這里直接使用之前的MyTask2類,不再重復(fù)寫(xiě)。
修改applicationContext.xml文件:
需要追加task處理的命名空間定義:
<beans xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd >
1配置task操作的配置,實(shí)現(xiàn)間隔觸發(fā)。
<bean id="myTask" class="cn.wnh.timerSask.MyTask2" />
<task:scheduled-tasks>
<task:scheduled ref="myTask" method="taskSelf"
fixed-rate="2000" />
</task:scheduled-tasks>
使用cron實(shí)現(xiàn)定時(shí)觸發(fā)
<bean id="myTask" class="cn.wnh.timerSask.MyTask2" />
<task:scheduled-tasks>
<task:scheduled ref="myTask" method="taskSelf" cron="* * * * * ?" />
</task:scheduled-tasks>
可見(jiàn),SpringTask實(shí)現(xiàn)起來(lái)更加簡(jiǎn)單。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java中Spring使用Quartz任務(wù)調(diào)度定時(shí)器
- spring利用squertz實(shí)現(xiàn)定時(shí)任務(wù)
- Spring整合Quartz實(shí)現(xiàn)動(dòng)態(tài)定時(shí)器的示例代碼
- spring task 定時(shí)任務(wù)實(shí)現(xiàn)示例
- Spring整合TimerTask實(shí)現(xiàn)定時(shí)任務(wù)調(diào)度
- Spring整合Quartz實(shí)現(xiàn)定時(shí)任務(wù)調(diào)度的方法
- 最流行的java后臺(tái)框架spring quartz定時(shí)任務(wù)
- JAVA中 Spring定時(shí)器的兩種實(shí)現(xiàn)方式
- java中 spring 定時(shí)任務(wù) 實(shí)現(xiàn)代碼
相關(guān)文章
SpringBoot 如何整合 ES 實(shí)現(xiàn) CRUD 操作
這篇文章主要介紹了SpringBoot 如何整合 ES 實(shí)現(xiàn) CRUD 操作,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-10-10
Mybatis開(kāi)發(fā)要點(diǎn)-resultType和resultMap有什么區(qū)別詳解
本文主要介紹了Mybatis開(kāi)發(fā)要點(diǎn)-resultType和resultMap有什么區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
圖解Java?ReentrantLock公平鎖和非公平鎖的實(shí)現(xiàn)
ReentrantLock是Java并發(fā)中十分常用的一個(gè)類,具備類似synchronized鎖的作用。但是相比synchronized,?它具備更強(qiáng)的能力,同時(shí)支持公平鎖和非公平鎖。本文就來(lái)聊聊ReentrantLock公平鎖和非公平鎖的實(shí)現(xiàn),需要的可以參考一下2022-10-10
MyBatis實(shí)現(xiàn)Mysql數(shù)據(jù)庫(kù)分庫(kù)分表操作和總結(jié)(推薦)
這篇文章主要介紹了MyBatis實(shí)現(xiàn)Mysql數(shù)據(jù)庫(kù)分庫(kù)分表操作和總結(jié),需要的朋友可以參考下2017-08-08
nacos配置實(shí)例權(quán)重不生效問(wèn)題
這篇文章主要介紹了nacos配置實(shí)例權(quán)重不生效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07

