您好,登录后才能下订单哦!
前言:
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。