SpringBoot基于数据库的定时任务统一管理
定时任务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<>();
/*
*模拟数据库存储
*/
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 {
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 执行其他类型的定时任务
}
}
微信公众号

SpringBoot基于数据库的定时任务统一管理的更多相关文章
- SpringBoot基于数据库的定时任务实现
在我们平时开发的项目中,定时任务基本属于必不可少的功能,那大家都是怎么做的呢?但我知道的大多都是静态定时任务实现. 基于注解来创建定时任务非常简单,只需几行代码便可完成.实现如下: @Configur ...
- SpringBoot基于数据库实现简单的分布式锁
本文介绍SpringBoot基于数据库实现简单的分布式锁. 1.简介 分布式锁的方式有很多种,通常方案有: 基于mysql数据库 基于redis 基于ZooKeeper 网上的实现方式有很多,本文主要 ...
- springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验--异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档---jpa访问数据库及page进行分页---整合redis---定时任务
springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验-- 异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档 ...
- SpringBoot微服务电商项目开发实战 --- 模块版本号统一管理及Redis集成实现
上一篇文章总结了基于SpringBoot实现分布式微服务下的统一配置.分环境部署配置.以及服务端模块的分离(每一个提供者就是一个独立的微服务).微服务落地.Dubbo整合及提供者.消费者的配置实现.本 ...
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
1.前言 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelp ...
- 基于Git的数据库sql文件的管理——完美解决团队sql操作协同问题
目录 基于Git的数据库sql文件的管理--完美解决团队sql操作协同问题 1.产生背景 2.之前没用Git管理数据库出现的问题 2.1 用同一个库调试带来的问题 3.解决方案 3.1 Sql文件的创 ...
- springboot 基于@Scheduled注解 实现定时任务
前言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...
- 图数据库HugeGraph:HugeGraph-Hubble基于Web的可视化图管理初体验
原创/朱季谦 一.HugeGraph-Hubble简介 关于HugeGraph,官方资料是这样介绍的,它是一款易用.高效.通用的开源图数据库系统(Graph Database), 实现了 Apache ...
- .NET Core微服务之基于Steeltoe使用Spring Cloud Config统一管理配置
Tip: 此篇已加入.NET Core微服务基础系列文章索引 => Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...
随机推荐
- poj 1279 Art Gallery (Half Plane Intersection)
1279 -- Art Gallery 还是半平面交的问题,要求求出多边形中可以观察到多边形所有边的位置区域的面积.其实就是把每一条边看作有向直线然后套用半平面交.这题在输入的时候应该用多边形的有向面 ...
- get和post的区别?
GET:一般用于信息获取,使用URL传递参数,对所发送信息的数量也有限制,一般在2000个字符 POST:一般用于 修改服务器上的资源,对所发送的信息没有限制. GET方式需要使用Request.Qu ...
- 【转载】.NET中使用Redis
Redis是一个用的比较广泛的Key/Value的内存数据库,新浪微博.Github.StackOverflow 等大型应用中都用其作为缓存,Redis的官网为http://redis.io/. 最近 ...
- JVM调优-Jstack线程分析
jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项"-J-d64",Windows的jstack使 ...
- HashMap之红黑树
红黑树的设计,相比 jdk1.7 的 HashMap 而言,jdk1.8 最重要的就是引入了红黑树的设计,当冲突的链表长度超过 8 个的时候,链表结构就会转为红黑树结构. 01.故事的起因 “ JDK ...
- LuoguP5464 缩小社交圈
LuoguP5464 缩小社交圈 背景:洛谷七月月赛T4 题目大意给定\(n\)个点,每个点的权值对应着一个区间\([l_i,r_i]\),两个点\(i,j\)有边当且仅当他们权值的并集不为空集,问有 ...
- 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...
- 阿里云 CentOS8 Repo
# CentOS-Base.repo # # The mirror system uses the connecting IP address of the client and the # upda ...
- mac系统上访问docker容器中的ip配置
使用 mac系统,发现docker没有 docker0网桥,无法直接在宿主机上 访问 容器的ip, 在测试的时候有这种需求,而不是通过-p的方式,可以参考下面的连接,主要就是 修改 setting.j ...
- 一目了然 | 数据库实例性能调优利器:Performance Insights
Performance Insights是什么 阿里云RDS Performance Insights是RDS CloudDBA产品一项专注于用户数据库实例性能调优.负载监控和关联分析的利器,以简单直 ...