您好,登錄后才能下訂單哦!
前言:
Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力于在蓬勃發展的快速應用開發領域(rapid application development)成為領導者。
從最根本上來講,Spring Boot就是一些庫的集合,它能夠被任意項目的構建系統所使用。簡便起見,該框架也提供了命令行界面,它可以用來運行和測試Boot應用。框架的發布版本,包括集成的CLI(命令行界面),可以在Spring倉庫中手動下載和安裝
代碼:
定時任務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 taskRecordList = new ArrayList<>();
/*
*模擬數據庫存儲
*/
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("執行定時任務,{}", LocalDateTime.now()), "0/5 * * * * ?");
/* taskRegistrar.addCronTask(new Runnable() {
@Override
public void run() {
try {鄭州中原婦科醫院 http://mobile.chfk120.com/
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 執行其他類型的定時任務
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。