SpringBoot 使用定时任务动态执行任务
import com.patient.core.adapter.CorsFilter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling//开启定时任务注解
@SpringBootApplication
@MapperScan("com.patient.core.mapper")
public class PatientSystemApplication { public static void main(String[] args) {
SpringApplication.run(PatientSystemApplication.class, args);
} @Bean
public FilterRegistrationBean setFilter() {
FilterRegistrationBean filterBean = new FilterRegistrationBean();
filterBean.setFilter(new CorsFilter());
filterBean.setName("CorsFilter");
filterBean.addUrlPatterns("/*");
return filterBean;
}
}
import com.patient.core.service.UserService;
import com.patient.core.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger; import java.util.Date; @Configuration
public class ReminderTask implements SchedulingConfigurer {
private static Logger log = LoggerFactory.getLogger(ReminderTask.class); @Autowired
private UserService userService;
//项目启动会自动执行该Controller 类,首先执行触发器的方法,查询要执行业务逻辑的时间(需要转换成Cron表达式),动态的执行业务逻辑,业务逻辑执行完毕会再次执行触发器 设定下次执行时间
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.addTriggerTask(doTask(), getTrigger());
} // 业务执行方法
private Runnable doTask() {
return new Runnable() {
@Override
public void run() {
try {
//业务逻辑
log.info("业务执行方法");
} catch (Exception e) {
e.printStackTrace();
}
}
};
} //业务触发器
private Trigger getTrigger() {
return new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
String dateStr = userService.getLastTime();
if ("".equals(dateStr) || dateStr == null)
return null;
String cron = DateUtils.getCron(DateUtils.strToDateLong(dateStr));
//定时任务触发,可修改定时任务的执行周期
CronTrigger trigger = new CronTrigger(cron);
Date nextExecDate = trigger.nextExecutionTime(triggerContext);
log.info("业务触发器:"+DateUtils.dateToStrLong(nextExecDate));
return nextExecDate;
}
};
}
SpringBoot 使用定时任务动态执行任务的更多相关文章
- springboot配置定时任务并发执行
@Configuration public class ScheduleConfig implements SchedulingConfigurer { @Override public void c ...
- Spring Boot 创建定时任务(配合数据库动态执行)
序言:创建定时任务非常简单,主要有两种创建方式:一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer). 前者相信大家都很熟悉,但是实际使用中我们往往想从数据库 ...
- springboot 不停服动态更新定时任务时间(转)
转 https://blog.csdn.net/u012129558/article/details/80834303 Spring框架自3.0版本起,自带了任务调度功能,好比是一个轻量级的Quart ...
- quart动态执行定时任务
今天有个需求,前端可以将定时任务自定义保存到数据库,每天根据查询数据库来执行任务. 其实不用动态也是可以实现,但是.也是想试试动态执行定时任务看看怎么样的. (1)建立一个QuartzManage类 ...
- 玩转SpringBoot之定时任务详解
序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...
- jeecg 3.7.1 新版功能,集群定时任务动态发布模块 使用规则
jeecg 3.7.1 集群定时任务动态发布模块 使用规则 新版特性: 支持集群定时任务,支持分布式. 菜单路径: 系统监控-->定时任务 字段说明: 任务ID.任务说明:自定义即可 ...
- Springboot 项目启动后执行某些自定义代码
Springboot 项目启动后执行某些自定义代码 Springboot给我们提供了两种"开机启动"某些方法的方式:ApplicationRunner和CommandLineRun ...
- SpringBoot 配置定时任务
SpringBoot启用定时任务,其内部集成了成熟的框架,因此我们可以很简单的使用它. 开启定时任务 @SpringBootApplication //设置扫描的组件的包 @ComponentScan ...
- SpringBoot - 添加定时任务
SpringBoot 添加定时任务 EXample1: import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spri ...
随机推荐
- 设置adb shell的环境变量
1.设置adb系统变量 adb D:\androidStudio\platform-tools;D:\androidStudio\tools 2.设置path系统变量 path D:\android ...
- O018、理解 Keystone 核心概念
参考https://www.cnblogs.com/CloudMan6/p/5365474.html 作为OpenStack的基础支持服务,Keystone做了下面几件事情: 1.管理 ...
- java实现spark常用算子之flatmap
import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaRDD;import org.apache.spark.a ...
- echarts属性的设置
// 全图默认背景 // backgroundColor: ‘rgba(0,0,0,0)’, // 默认色板 color: ['#ff7f50','#87cefa','#da70d6','#32cd ...
- 仿造email后缀搜索功能(2)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- c++ 实时通信系统(基础知识TCP/IP篇)
编写前的基础知识 C/S结构: C/S是Client/Server,即客户端/服务器端架构,一种典型的两层架构.客户端包含一个或多个在用户的电脑上运行的程序服务器端有两种,一种是数据库服务器端,客户端 ...
- django笔记一
课程介绍 MVC 大部分开发语言中都有MVC框架 MVC框架的核心思想是:解耦 降低各功能模块之间的耦合性,方便变更,更容易重构代码,最大程度上实现代码的重用 m表示model,主要用于对数据库层的封 ...
- jmeter通过if控制器控制业务比例
以发帖,看帖,回帖三个事物为例,这里就10个用户跑10次,进行测试下: 可以看到看帖,回帖,发帖比例是5:3:2,先来说说怎么做到的,就是通过if控制器,分别来看下几个控制器的内容, 看帖(if控制器 ...
- H5 2次重定向301
测试环境http ,生产https,导致请求重定向.加之H5自己的一次请求重定向,一共2次.
- python ddt及logging(九)
一.安装 ①执行语句: pip install ddt ②在单元测试中DDT用来进行数据驱动,数据与测试代码分离(数据发生变化时,用例可以保持不变),一组数据来执行相同的操作. 一般进行接口测试时,每 ...