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 ...
随机推荐
- 类的函数成员之属性property
属性命名采用Pascal命名方式,每个单词的首字母大写.访问方式与访问类的公共字段类似. /// <summary> /// 字段 /// </summary> private ...
- uoj #46[清华集训2014]玄学
uoj 因为询问是关于一段连续区间内的操作的,所以对操作构建线段树,这里每个点维护若干个不交的区间,每个区间\((l,r,a,b)\)表示区间\([l,r]\)内的数要变成\(ax+b\) 每次把新操 ...
- websocket具体如何使用
本人是在https://blog.csdn.net/jintingbo/article/details/80755636此地址学习的,所以留做笔记用于之后的学习 现在在写一个工程,是关于监控摄像头的, ...
- 微软内部封杀 Slack
就在 Slack 在股市上亮相之际,有一家大公司却不允许员工在日常工作中使用这款企业协作和聊天应用软件. 微软已禁止其 100000 多名员工使用免费版 Slack.IT 外媒 GeekWire 报道 ...
- Rinetd 通过ECS端口转发到内网RDS
前置条件 实现目的:开发本地电脑需要连接没有外网地址的RDS,通过ECS进行转发连接到RDS数据库 客户 PC 终端可以 ssh 登录有公网的 ECS 服务器. 有公网的 ECS 服务器可以通过内网访 ...
- linux pip使用国内源
最近在Linux里面使用pip安装应用的速度十分的慢,于是便上网找了一些国内的源. 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:https:// ...
- Python测开面试题之装饰器
Python的装饰器是面试常被问到的问题之一,在面试Python测试开发时被问到的概率不低于70%,那么装饰器的原理是什么,怎么快速写出一个装饰器呢,接下来我们详细讲解装饰器的实现方法. Python ...
- jmeter解析response里的json对象和数组
1.解析提取json对象 2.解析提取json数组 注意,标红这里是从0开始计数 提取最后一个数组
- pidstat 命令(Linux 进程使用资源情况采样)
pidstat 作用 pidstat 获取服务器指定进程的使用资源信息(包括 CPU.设备IO.内存.线程.任务切换等). 执行一波 [root@wille ~]# pidstat Linux 2.6 ...
- nicstat命令安装与分析
nicstat安装包下载与安装: wget https://downloads.sourceforge.net/project/nicstat/nicstat-1.95.tar.gz tar -zxv ...