SpringBoot基于數(shù)據(jù)庫(kù)的定時(shí)任務(wù)統(tǒng)一管理的實(shí)現(xiàn)
定時(shí)任務(wù)1
import lombok.extern.slf4j.Slf4j;
/**
* @author Created by niugang on 2019/12/24/15:29
*/
@Slf4j
public class TaskTest {
public void task1() {
log.info("反射調(diào)用測(cè)試[一]類");
}
}
定時(shí)任務(wù)2
import lombok.extern.slf4j.Slf4j;
/**
* @author Created by niugang on 2019/12/24/15:54
*/
@Slf4j
public class TaskTest2 {
public void task2() {
log.info("反射調(diào)用測(cè)試[二]類");
}
}
配置類
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.ScheduledTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* @author Created by niugang on 2019/12/24/15:19
*/
@Configuration
@EnableScheduling
@Slf4j
public class CompleteScheduleConfig implements SchedulingConfigurer {
private static List<TaskRecord> taskRecordList = new ArrayList<>();
/*
*模擬數(shù)據(jù)庫(kù)存儲(chǔ)
*/
static {
TaskRecord taskRecord = new TaskRecord();
taskRecord.setExecuteMehod("task1");
taskRecord.setClassPath("com.example.demo.pojo.TaskTest");
taskRecord.setCron("0/5 * * * * ?");
taskRecordList.add(taskRecord);
TaskRecord taskRecord2 = new TaskRecord();
taskRecord2.setExecuteMehod("task2");
taskRecord2.setClassPath("com.example.demo.pojo.TaskTest2");
taskRecord2.setCron("0/10 * * * * ?");
taskRecordList.add(taskRecord2);
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
// taskRegistrar.addCronTask(() -> log.info("執(zhí)行定時(shí)任務(wù),{}", LocalDateTime.now()), "0/5 * * * * ?");
/* taskRegistrar.addCronTask(new Runnable() {
@Override
public void run() {
try {
Class<?> aClass = Class.forName("com.example.demo.pojo.TaskTest");
Object o = aClass.newInstance();
Method[] declaredMethods = aClass.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
declaredMethod.invoke(o);
// log.info("方法名稱:{}",declaredMethod.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, "0/5 * * * * ?");*/
for (TaskRecord taskRecord : taskRecordList) {
String classPath = taskRecord.getClassPath();
String cron = taskRecord.getCron();
String executeMehod = taskRecord.getExecuteMehod();
Runnable runnable = () -> {
Class<?> aClass;
try {
aClass = Class.forName(classPath);
Object o = aClass.newInstance();
Method[] declaredMethods = aClass.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
if (declaredMethod.getName().equals(executeMehod)) {
/// log.info("方法名稱:{}",declaredMethod.getName());
declaredMethod.invoke(o);
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
};
CronTask cronTask = new CronTask(runnable, cron);
ScheduledTask scheduledTask = taskRegistrar.scheduleCronTask(cronTask);
//scheduledTask.cancel(); 取消定時(shí)任務(wù)
}
}
@Data
private static class TaskRecord {
private String classPath;
private String executeMehod;
private String cron;
//可以在增加一個(gè)type 執(zhí)行其他類型的定時(shí)任務(wù)
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)動(dòng)態(tài)管理示例
- Springboot-admin整合Quartz實(shí)現(xiàn)動(dòng)態(tài)管理定時(shí)任務(wù)的過(guò)程詳解
- SpringBoot日程管理Quartz與定時(shí)任務(wù)Task實(shí)現(xiàn)詳解
- SpringBoot實(shí)現(xiàn)quartz定時(shí)任務(wù)可視化管理功能
- SpringBoot中使用Quartz管理定時(shí)任務(wù)的方法
- Springboot實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)管理的示例代碼
相關(guān)文章
SpringBoot實(shí)現(xiàn)AOP切面的三種方式
Spring,SpringBoot框架憑借多種高效機(jī)制,顯著增強(qiáng)了代碼的功能性,并實(shí)現(xiàn)了切面編程(AOP)的精髓,其核心亮點(diǎn)之一,是運(yùn)用動(dòng)態(tài)代理技術(shù),無(wú)需觸動(dòng)源代碼即可在Bean的運(yùn)行時(shí)為其動(dòng)態(tài)織入額外功能,本文給大家介紹了SpringBoot通過(guò)3種方式實(shí)現(xiàn)AOP切面,需要的朋友可以參考下2024-08-08
Docker?DockerFile部署java?jar項(xiàng)目包及Mysql和Redis的詳細(xì)過(guò)程
Dockerfile是一種用于構(gòu)建Docker鏡像的文件格式,可以通過(guò)Dockerfile部署Java項(xiàng)目,這篇文章主要給大家介紹了關(guān)于Docker?DockerFile部署java?jar項(xiàng)目包及Mysql和Redis的詳細(xì)過(guò)程,需要的朋友可以參考下2023-12-12
Mybatis如何使用正則模糊匹配多個(gè)數(shù)據(jù)
這篇文章主要介紹了Mybatis如何使用正則模糊匹配多個(gè)數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
SpringBoot解析指定Yaml配置文件的實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了SpringBoot解析指定Yaml配置文件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(48)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-08-08
Java?SpringBoot集成文件之如何使用POI導(dǎo)出Word文檔
這篇文章主要介紹了Java?SpringBoot集成文件之如何使用POI導(dǎo)出Word文檔,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08

