1、概述: 在開(kāi)發(fā)中有的時(shí)候需要去手動(dòng)禁止和啟用定時(shí)任務(wù),修改定時(shí)任務(wù)的cron表達(dá)式然后再讓其動(dòng)態(tài)生效,之前有過(guò)ssm的類似的業(yè)務(wù)的開(kāi)發(fā)但是忘記寫下來(lái)了。。。只好重新溫習(xí)了一次,加上最近比較流行springboot所以升級(jí)了一下用springboot來(lái)完成.
2、關(guān)聯(lián)技術(shù) springboot、quartz、h2、thymeleaf (好像就這么多)
3、具體流程
1)首先去手動(dòng)創(chuàng)建一個(gè)調(diào)度器工廠對(duì)象-schedulerfactorybean;其實(shí)應(yīng)該不用手動(dòng)創(chuàng)建的但是為了顧及到業(yè)務(wù)的復(fù)雜性所以還是創(chuàng)建一個(gè)好用。
1
2
3
4
5
6
7
8
9
|
@bean public schedulerfactorybean schedulerfactory(){ schedulerfactorybean factorybean = new schedulerfactorybean(); /*用于quartz集群,啟動(dòng)時(shí)更新已存在的job*/ factorybean.setoverwriteexistingjobs(true); /*定時(shí)任務(wù)開(kāi)始啟動(dòng)后延遲5秒開(kāi)始*/ factorybean.setstartupdelay( 5 ); return factorybean; } |
2)獲取到
1
2
|
//得到調(diào)度器 scheduler scheduler = schedulerfactorybean.getscheduler(); |
3)判斷是否有觸發(fā)器-trigger存在其中,因?yàn)橛锌赡苷f(shuō)上次的觸發(fā)器 并沒(méi)有刪除
1
2
3
|
//獲得觸發(fā)器 triggerkey triggerkey = triggerkey.triggerkey(config.getname(), config.getgroup()); crontrigger trigger = (crontrigger)scheduler.gettrigger(triggerkey); |
4)創(chuàng)建一個(gè)任務(wù)類需要繼承job,實(shí)現(xiàn)方法execute。需要在其中執(zhí)行定時(shí)任務(wù)如下:
1
2
3
4
5
6
7
8
9
10
11
|
//注釋作用,當(dāng)上一個(gè)任務(wù)未結(jié)束時(shí)下一個(gè)任務(wù)需進(jìn)行等待 @disallowconcurrentexecution public class quartzjobfactory implements job { public static final string schedulejobkey= "schedulejob" ; //execute會(huì)根據(jù)cron的規(guī)則進(jìn)行執(zhí)行 @override public void execute(jobexecutioncontext jobexecutioncontext) throws jobexecutionexception { config config = (config) jobexecutioncontext.getmergedjobdatamap().get(schedulejobkey); taskutils.invokmethod(config); } } |
5)將執(zhí)行實(shí)例添加到任務(wù)當(dāng)中去,我在例子是將執(zhí)行任務(wù)的信息封裝到了對(duì)象config當(dāng)中然后在任務(wù)quartzjobfactoryz中進(jìn)行解讀的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public static void invokmethod(config config){ object obj= null ; class clazz= null ; //通過(guò)spring上下文去找 也有可能找不到 try { obj= springutils.getbean(config.getclasspath().split( "\\." )[config.getclasspath().split( "\\." ).length - 1 ]); if (obj == null ){ clazz = class .forname(config.getclasspath()); obj = clazz.newinstance(); } else { clazz =obj.getclass(); } } catch (exception e){ throw new runtimeexception( "error:taskutils is bean create please check the classpath is`t right or not" ); } method method= null ; //獲得方法名 try { method = clazz.getdeclaredmethod(config.getmethodname()); } catch (nosuchmethodexception e) { throw new runtimeexception( "error:taskutils is bean the method create please check the methodname is`t right or not" ); } //方法執(zhí)行 try { method.invoke(obj); } catch (exception e) { throw new runtimeexception( "error:taskutils is bean the method execute please check the methodname is`t right or not" ); } } |
6)創(chuàng)建觸發(fā)器并且綁定cron表達(dá)式
7)在調(diào)度器中將觸發(fā)器和任務(wù)進(jìn)行組合 詳情見(jiàn):com.study.www.service.quartztableservice.addjob
1
2
3
4
5
6
|
//將cron表達(dá)式進(jìn)行轉(zhuǎn)換 cronschedulebuilder cronschedulebuilder = cronschedulebuilder.cronschedule(config.getcron()); //創(chuàng)建觸發(fā)器并將cron表達(dá)式對(duì)象給塞入 trigger = triggerbuilder.newtrigger().withidentity(triggerkey).withschedule(cronschedulebuilder).build(); //在調(diào)度器中將觸發(fā)器和任務(wù)進(jìn)行組合 scheduler.schedulejob(jobdetail,trigger); |
github:點(diǎn)擊打開(kāi)鏈接
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/zhu714702382/article/details/79391437