SpringBoot基于數(shù)據(jù)庫的定時任務統(tǒng)一管理的實現(xiàn)
更新時間:2019年12月26日 11:58:17 作者:盲目的拾荒者
這篇文章主要介紹了SpringBoot基于數(shù)據(jù)庫的定時任務統(tǒng)一管理的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
定時任務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("反射調用測試[一]類");
}
}
定時任務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("反射調用測試[二]類");
}
}
配置類
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ù)庫存儲
*/
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í)行定時任務,{}", 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(); 取消定時任務
}
}
@Data
private static class TaskRecord {
private String classPath;
private String executeMehod;
private String cron;
//可以在增加一個type 執(zhí)行其他類型的定時任務
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Docker?DockerFile部署java?jar項目包及Mysql和Redis的詳細過程
Dockerfile是一種用于構建Docker鏡像的文件格式,可以通過Dockerfile部署Java項目,這篇文章主要給大家介紹了關于Docker?DockerFile部署java?jar項目包及Mysql和Redis的詳細過程,需要的朋友可以參考下2023-12-12
Mybatis如何使用正則模糊匹配多個數(shù)據(jù)
這篇文章主要介紹了Mybatis如何使用正則模糊匹配多個數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
SpringBoot解析指定Yaml配置文件的實現(xiàn)過程
這篇文章主要介紹了SpringBoot解析指定Yaml配置文件,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
Java?SpringBoot集成文件之如何使用POI導出Word文檔
這篇文章主要介紹了Java?SpringBoot集成文件之如何使用POI導出Word文檔,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08

