spring schedule實(shí)現(xiàn)動態(tài)配置執(zhí)行時(shí)間
spring schedule 動態(tài)配置執(zhí)行時(shí)間
之前saas平臺實(shí)現(xiàn)動態(tài)修改定時(shí)任務(wù)的時(shí)間,都是通過xx-job這樣的框架來實(shí)現(xiàn),這樣我們可以單獨(dú)一個(gè)服務(wù)來管理我們整個(gè)saas平臺的定時(shí)任務(wù),但是最近給銀行做的一個(gè)小項(xiàng)目,需要本地化部署,所以我不想弄很多的服務(wù),并且他們并沒有要求修改以后即時(shí)生效,所以我直接采用了 spring schedule結(jié)合mysql動態(tài)配置執(zhí)行時(shí)間。
之前我們用的schedule通過注解的方式,只能用靜態(tài)的corn表達(dá)式,如果想實(shí)現(xiàn)動態(tài)的需要實(shí)現(xiàn)SchedulingConfigurer,并且通過注解@EnableScheduling。如下:
package com.zqf.marketing.task; import com.zqf.db.marketingrobot.sys.model.RobotSysSwitch; import com.zqf.marketing.sys.service.SwitchService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.scheduling.Trigger; import org.springframework.scheduling.TriggerContext; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.support.CronTrigger; import org.springframework.stereotype.Service; import java.util.Date; /** * @author zhenghao * @description * @date 2019/1/22 21:50 */ @Lazy(false) @Service @EnableScheduling public class TestTaskService implements SchedulingConfigurer { private static Logger log = LoggerFactory.getLogger(TestTaskService.class); @Autowired private SwitchService switchService; private String SpringDynamicCronTask() { String cron = "0/5 * * * * ?"; //從數(shù)據(jù)庫獲得配置的corn表達(dá)式 RobotSysSwitch switchById = switchService.getSwitchById(5L); cron = switchById.getSwitchFlag(); log.info(cron); return cron; } @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { scheduledTaskRegistrar.addTriggerTask(new Runnable() { @Override public void run() { // 任務(wù)邏輯 log.info("task_task_tak"); } }, new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) { String s = SpringDynamicCronTask(); // 任務(wù)觸發(fā),可修改任務(wù)的執(zhí)行周期 CronTrigger trigger = new CronTrigger(s); Date nextExec = trigger.nextExecutionTime(triggerContext); return nextExec; } }); } }
這樣我們就可以動態(tài)的修改task的執(zhí)行時(shí)間,生效時(shí)間為,上一個(gè)任務(wù)的執(zhí)行周期,也可以滿足我們現(xiàn)在需求,這樣就可以實(shí)習(xí)項(xiàng)目更加的靈活!
@schedule注解動態(tài)配置時(shí)間間隔
動態(tài)配置時(shí)間間隔是通過自己實(shí)現(xiàn)的任務(wù)注冊到任務(wù)調(diào)度實(shí)現(xiàn)的,并在每次調(diào)度的時(shí)候更改下次調(diào)度時(shí)間間隔,如果任務(wù)阻塞或者掛掉了就不會再被調(diào)度了,如果設(shè)置時(shí)間過長,到下次調(diào)度就需要等待很長時(shí)間。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.Trigger; import org.springframework.scheduling.TriggerContext; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.support.PeriodicTrigger; import org.springframework.stereotype.Component; import java.util.Date; @Component @EnableScheduling public class DynamicScheduleTaskSecond implements SchedulingConfigurer { private static final long WEEK_MILLIS = 604800000; private static final long MIN_MILLIS = 1000; private static long period = 1000; static long l = System.currentTimeMillis(); @Autowired SetPeriod setPeriod; public static long getPeriod() { return period; } public static void setPeriod(long period) { DynamicScheduleTaskSecond.period = period; } @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addTriggerTask(new Runnable() { @Override public void run() { try { setPeriod.update(period); System.out.println("abc"); Long last = System.currentTimeMillis() - l; l = System.currentTimeMillis(); System.out.println(last); } catch (Exception e) { e.printStackTrace(); } } }, new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) { if (period < MIN_MILLIS || period > WEEK_MILLIS) period = MIN_MILLIS; PeriodicTrigger periodicTrigger = new PeriodicTrigger(period); Date nextExecDate = periodicTrigger.nextExecutionTime(triggerContext); return nextExecDate; } }); } }
import org.springframework.stereotype.Component; @Component public class SetPeriod { private static Long maxPeriod = 1000l; public void update(Long period) { maxPeriod += 1000; setScheduleConfig(maxPeriod); } public boolean setScheduleConfig(Long period) { DynamicScheduleTaskSecond.setPeriod(period); return true; } }
上面是實(shí)現(xiàn)動態(tài)調(diào)度的一個(gè)簡單實(shí)例,下面說一下基本原理。
動態(tài)調(diào)度功能主要是實(shí)現(xiàn)SchedulingConfigurer函數(shù)式接口,接口中的方法configureTasks的參數(shù)是重點(diǎn)。
@FunctionalInterface public interface SchedulingConfigurer { /** * Callback allowing a {@link org.springframework.scheduling.TaskScheduler * TaskScheduler} and specific {@link org.springframework.scheduling.config.Task Task} * instances to be registered against the given the {@link ScheduledTaskRegistrar}. * @param taskRegistrar the registrar to be configured. */ void configureTasks(ScheduledTaskRegistrar taskRegistrar); }
看名字 ScheduledTaskRegistrar就知道是一個(gè)調(diào)度任務(wù)注冊類,調(diào)用這個(gè)類的addTriggerTask方法需要兩個(gè)參數(shù)
public void addTriggerTask(Runnable task, Trigger trigger) { this.addTriggerTask(new TriggerTask(task, trigger)); }
一個(gè)是任務(wù)線程這個(gè)最后說,先說一下第二個(gè)Trigger,這是一個(gè)設(shè)置任務(wù)觸發(fā)時(shí)間的接口,具體的實(shí)現(xiàn)有兩個(gè)類,一個(gè)是CronTrigger對應(yīng)的就是cron類型的時(shí)間設(shè)置,一個(gè)是PeriodicTrigger對應(yīng)的就是FixDelay和FixRate兩種方式的時(shí)間設(shè)置,實(shí)例中使用的是后者。
public interface Trigger { @Nullable Date nextExecutionTime(TriggerContext var1); }
接口方法參數(shù)是一個(gè)TriggerContext,這個(gè)參數(shù)就是任務(wù)觸發(fā)的上下文,里面保存著上一次任務(wù)開始時(shí)間和結(jié)束時(shí)間和實(shí)際執(zhí)行用時(shí),自己需要實(shí)現(xiàn)這個(gè)nextExecutionTime方法根據(jù)上一次任務(wù)執(zhí)行時(shí)間來返回一個(gè)新的Date時(shí)間,new一個(gè)新的periodicTrigger對象初始化period時(shí)間間隔為新的時(shí)間間隔用nextExecutionTime方法就可以了根據(jù)上下文時(shí)間返回一個(gè)新的任務(wù)調(diào)度時(shí)間了,但是period的時(shí)間不能太長也不能太短最好設(shè)置一個(gè)區(qū)間,這樣可以避免很多粗心的錯(cuò)誤導(dǎo)致的麻煩,到此完美解決動態(tài)設(shè)置任務(wù)調(diào)度時(shí)間間隔功能。
再說一下第一個(gè)線程任務(wù)中的需要做的事,執(zhí)行的任務(wù)需要在其他的具體類中實(shí)現(xiàn),然后在這個(gè)線程中調(diào)用,然后每次在調(diào)度任務(wù)的時(shí)候就要根據(jù)時(shí)間業(yè)務(wù)重新設(shè)置時(shí)間間隔,比如讀配置后改變時(shí)間間隔,也就是調(diào)度和具體的任務(wù)形成一個(gè)環(huán),調(diào)度執(zhí)行具體的任務(wù)后,具體的任務(wù)在設(shè)置調(diào)度的時(shí)間間隔。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)鼠標(biāo)模擬與鍵盤映射
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)鼠標(biāo)模擬與鍵盤映射,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08總結(jié)Java集合類操作優(yōu)化經(jīng)驗(yàn)
本文主要介紹的就是集合框架的使用經(jīng)驗(yàn),告訴大家如何高效、方便地管理對象,所有代碼基于JDK7,需要的朋友可以參考下2015-08-08關(guān)于logback日志級別動態(tài)切換的四種方式
這篇文章主要介紹了關(guān)于logback日志級別動態(tài)切換的四種方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Windows下將JAVA?jar注冊成windows服務(wù)的方法
這篇文章主要介紹了Windows下將JAVA?jar注冊成windows服務(wù)的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07Mybatis查詢時(shí)數(shù)據(jù)丟失的問題及解決
Mybatis查詢時(shí)數(shù)據(jù)丟失的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java實(shí)現(xiàn)軟件運(yùn)行時(shí)啟動信息窗口的方法
這篇文章主要介紹了Java實(shí)現(xiàn)軟件運(yùn)行時(shí)啟動信息窗口的方法,比較實(shí)用的功能,需要的朋友可以參考下2014-08-08