SpringBoot實(shí)現(xiàn)設(shè)置動(dòng)態(tài)定時(shí)任務(wù)的方法詳解
一、第一種
1. 引入依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<optional>true</optional>
</dependency>
<!-- spring boot 2.3版本后,如果需要使用校驗(yàn),需手動(dòng)導(dǎo)入validation包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
2. 添加注解
@EnableScheduling
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("(*^▽^*)啟動(dòng)成功!!!(〃'▽'〃)");
}
}
3. application.yml
只定義了服務(wù)端口:
server: port: 8089
4. task-config.ini
定時(shí)任務(wù)執(zhí)行時(shí)間配置文件:task-config.ini:
printTime.cron=0/10 * * * * ?
5. 定時(shí)任務(wù)執(zhí)行類
package com.gblfy.demo.task;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.Date;
/**
* 定時(shí)任務(wù)
* @author gblfy
*/
@Data
@Slf4j
@Component
@PropertySource("classpath:/task-config.ini")
public class ScheduleTask implements SchedulingConfigurer {
@Value("${printTime.cron}")
private String cron;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
// 動(dòng)態(tài)使用cron表達(dá)式設(shè)置循環(huán)間隔
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
log.info("Current time: {}", LocalDateTime.now());
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
// 使用CronTrigger觸發(fā)器,可動(dòng)態(tài)修改cron表達(dá)式來操作循環(huán)規(guī)則
CronTrigger cronTrigger = new CronTrigger(cron);
Date nextExecutionTime = cronTrigger.nextExecutionTime(triggerContext);
return nextExecutionTime;
}
});
}
}
6. 編寫一個(gè)接口
使得可以通過調(diào)用接口動(dòng)態(tài)修改該定時(shí)任務(wù)的執(zhí)行時(shí)間:
package com.face.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author gblfy
*/
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
private final ScheduleTask scheduleTask;
@Autowired
public TestController(ScheduleTask scheduleTask) {
this.scheduleTask = scheduleTask;
}
//http://localhost:8089/test/updateCron?cron=0/5%20*%20*%20*%20*%20?
@GetMapping("/updateCron")
public String updateCron(String cron) {
log.info("new cron :{}", cron);
scheduleTask.setCron(cron);
return "ok";
}
//http://localhost:8089/test/updateTimer?timer=5000
@GetMapping("/updateTimer")
public String updateTimer(Long timer) {
log.info("new timer :{}", timer);
scheduleTask.setTimer(timer);
return "ok";
}
}
啟動(dòng)項(xiàng)目,可以看到任務(wù)每10秒執(zhí)行一次: 訪問接口,傳入請求參數(shù)cron表達(dá)式,將定時(shí)任務(wù)修改為15秒執(zhí)行一次: 可以看到任務(wù)變成了15秒執(zhí)行一次
http://localhost:8089/test/updateCron?cron=0/5 * * * * ?
二、第二種
除了上面的借助cron表達(dá)式的方法,還有另一種觸發(fā)器,區(qū)別于CronTrigger觸發(fā)器,該觸發(fā)器可隨意設(shè)置循環(huán)間隔時(shí)間,不像cron表達(dá)式只能定義小于等于間隔59秒。
2.1. 調(diào)整定時(shí)任務(wù)類
package com.face.controller;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.Date;
/**
* 定時(shí)任務(wù)
* @author wl
*/
@Data
@Slf4j
@Component
@PropertySource("classpath:/task-config.ini")
public class ScheduleTask implements SchedulingConfigurer {
@Value("${printTime.cron}")
private String cron;
private Long timer = 10000L;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
// 動(dòng)態(tài)使用cron表達(dá)式設(shè)置循環(huán)間隔
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
log.info("Current time: {}", LocalDateTime.now());
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
// 使用CronTrigger觸發(fā)器,可動(dòng)態(tài)修改cron表達(dá)式來操作循環(huán)規(guī)則
// CronTrigger cronTrigger = new CronTrigger(cron);
// Date nextExecutionTime = cronTrigger.nextExecutionTime(triggerContext);
// 使用不同的觸發(fā)器,為設(shè)置循環(huán)時(shí)間的關(guān)鍵,區(qū)別于CronTrigger觸發(fā)器,該觸發(fā)器可隨意設(shè)置循環(huán)間隔時(shí)間,單位為毫秒
PeriodicTrigger periodicTrigger = new PeriodicTrigger(timer);
Date nextExecutionTime = periodicTrigger.nextExecutionTime(triggerContext);
return nextExecutionTime;`在這里插入代碼片`
}
});
}
}
2.2. 添加更新方法
@GetMapping("/updateTimer")
public String updateTimer(Long timer) {
log.info("new timer :{}", timer);
scheduleTask.setTimer(timer);
return "ok";
}
2.3. 測試
//localhost:8089/test/updateTimer?timer=5000
三、推薦方案
上面有很多弊端,弊端定時(shí)任務(wù)監(jiān)控,單機(jī)故障等等,建議采用分布式任務(wù)調(diào)度框架xxl-job
到此這篇關(guān)于SpringBoot實(shí)現(xiàn)設(shè)置動(dòng)態(tài)定時(shí)任務(wù)的方法詳解的文章就介紹到這了,更多相關(guān)SpringBoot設(shè)置動(dòng)態(tài)定時(shí)任務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)
- SpringBoot 動(dòng)態(tài)定時(shí)器的使用方法
- 淺談SpringBoot集成Quartz動(dòng)態(tài)定時(shí)任務(wù)
- SpringBoot設(shè)置動(dòng)態(tài)定時(shí)任務(wù)的方法詳解
- SpringBoot實(shí)現(xiàn)固定和動(dòng)態(tài)定時(shí)任務(wù)的三種方法
- SpringBoot創(chuàng)建動(dòng)態(tài)定時(shí)任務(wù)的幾種方式小結(jié)
- SpringBoot+Quartz實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)
- Springboot實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)流程詳解
- SpringBoot動(dòng)態(tài)定時(shí)任務(wù)實(shí)現(xiàn)完整版
- Springboot實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)管理的示例代碼
- 基于Nacos實(shí)現(xiàn)SpringBoot動(dòng)態(tài)定時(shí)任務(wù)調(diào)度
相關(guān)文章
通過spring注解開發(fā),簡單測試單例和多例區(qū)別
這篇文章主要介紹了通過spring注解開發(fā),簡單測試單例和多例區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
詳解如何使用java實(shí)現(xiàn)Open Addressing
這篇文章主要介紹了詳解如何使用java實(shí)現(xiàn)Open Addressing,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
SpringBoot整合Apache Ignite的實(shí)現(xiàn)
本文主要介紹了SpringBoot整合Apache Ignite的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
idea maven 項(xiàng)目src下的配置文件沒有同步至target的解決操作
這篇文章主要介紹了idea maven 項(xiàng)目src下的配置文件沒有同步至target的解決操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Spring中使用AOP進(jìn)行事務(wù)管理實(shí)例
這篇文章主要介紹了Spring中使用AOP進(jìn)行事務(wù)管理實(shí)例,當(dāng)在Spring項(xiàng)目中涉及數(shù)據(jù)庫操作時(shí),事務(wù)管理是非常重要的,它可以確保數(shù)據(jù)庫操作的一致性和完整性,Spring提供了強(qiáng)大的事務(wù)管理功能,可以通過聲明式或編程式兩種方式進(jìn)行配置,需要的朋友可以參考下2023-09-09
Spring Boot啟動(dòng)流程斷點(diǎn)過程解析
這篇文章主要介紹了Spring Boot啟動(dòng)流程斷點(diǎn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Java Web制作登錄驗(yàn)證碼實(shí)現(xiàn)代碼解析
這篇文章主要介紹了Java Web制作登錄驗(yàn)證碼實(shí)現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09

