SpringBoot實(shí)現(xiàn)動態(tài)定時任務(wù)的示例代碼
前言
之前在SpringBoot項(xiàng)目中簡單使用定時任務(wù),不過由于要借助cron表達(dá)式且都提前定義好放在配置文件里,不能在項(xiàng)目運(yùn)行中動態(tài)修改任務(wù)執(zhí)行時間,實(shí)在不太靈活?,F(xiàn)在我們就來實(shí)現(xiàn)可以動態(tài)修改cron表達(dá)式的定時任務(wù)。
配置文件
application-task.yml,其余的配置 application.yml 等就按照springBoot正常配置即可
task: cron: 0/10 * * * * ? timer: 10
定時任務(wù)核心類
import cn.hutool.core.date.DateUtil; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; 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 java.util.Date; @Data @Slf4j @Configuration @EnableScheduling @ConfigurationProperties(prefix = "task") public class WorkScheduleTask implements SchedulingConfigurer { private String cron; private Long timer; @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { // 動態(tài)使用cron表達(dá)式設(shè)置循環(huán)間隔 taskRegistrar.addTriggerTask(() -> { String dateTime = DateUtil.formatDateTime(new Date()); String threadName = Thread.currentThread().getName(); log.info("定時任務(wù)開始[configureTasks] :{},線程:{}", dateTime, threadName); }, triggerContext -> { // 使用CronTrigger觸發(fā)器,可動態(tài)修改cron表達(dá)式來操作循環(huán)規(guī)則 // 只能定義小于等于間隔59秒 // CronTrigger cronTrigger = new CronTrigger(cron); // return cronTrigger.nextExecutionTime(triggerContext); // 能定義大于等于間隔59秒 // 使用不同的觸發(fā)器,為設(shè)置循環(huán)時間的關(guān)鍵,區(qū)別于CronTrigger觸發(fā)器, // 該觸發(fā)器可隨意設(shè)置循環(huán)間隔時間,單位為毫秒 long seconds = timer * 1000; // 毫秒轉(zhuǎn)秒 PeriodicTrigger periodicTrigger = new PeriodicTrigger(seconds); return periodicTrigger.nextExecutionTime(triggerContext); }); } }
提供修改cron表達(dá)式的controller
@Slf4j @CrossOrigin @RestController @RequestMapping("/updateTask") public class UpdateTaskController { @Resource private WorkScheduleTask workScheduleTask; @PostMapping("/updateCron") public String updateCron(String cron) { log.info("new cron :{}", cron); workScheduleTask.setCron(cron); return "ok"; } @PostMapping("/updateTimer") public String updateTimer(Long timer) { log.info("new timer :{}", timer); workScheduleTask.setTimer(timer); return "ok"; } }
一開始定時任務(wù)的執(zhí)行時機(jī)和周期都是配置文件指定的,但是我們?nèi)绻麑τ趫?zhí)行的周期不滿意,我們可以調(diào)用接口進(jìn)行修改定時任務(wù),但是需要注意的是,這種外暴露的接最好做一下安全校驗(yàn),不是誰都可以調(diào)用,否則被別人掃描到這個接口,然后隨意修改,會影響我們正常的業(yè)務(wù)流程,嚴(yán)重可能會造成嚴(yán)重?fù)p失。
到此這篇關(guān)于SpringBoot實(shí)現(xiàn)動態(tài)定時任務(wù)的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot動態(tài)定時任務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Lombok 的@StandardException注解解析
@StandardException 是一個實(shí)驗(yàn)性的注解,添加到 Project Lombok 的 v__1.18.22 版本中,在本教程中,我們將使用 Lombok 的 @StandardException 注解自動生成異常類型類的構(gòu)造函數(shù),需要的朋友可以參考下2023-05-05Java Kafka分區(qū)發(fā)送及消費(fèi)實(shí)戰(zhàn)
本文主要介紹了Kafka分區(qū)發(fā)送及消費(fèi)實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07