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 使用定时任务动态执行任务的更多相关文章

  1. springboot配置定时任务并发执行

    @Configuration public class ScheduleConfig implements SchedulingConfigurer { @Override public void c ...

  2. Spring Boot 创建定时任务(配合数据库动态执行)

    序言:创建定时任务非常简单,主要有两种创建方式:一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer). 前者相信大家都很熟悉,但是实际使用中我们往往想从数据库 ...

  3. springboot 不停服动态更新定时任务时间(转)

    转 https://blog.csdn.net/u012129558/article/details/80834303 Spring框架自3.0版本起,自带了任务调度功能,好比是一个轻量级的Quart ...

  4. quart动态执行定时任务

    今天有个需求,前端可以将定时任务自定义保存到数据库,每天根据查询数据库来执行任务. 其实不用动态也是可以实现,但是.也是想试试动态执行定时任务看看怎么样的. (1)建立一个QuartzManage类 ...

  5. 玩转SpringBoot之定时任务详解

    序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...

  6. jeecg 3.7.1 新版功能,集群定时任务动态发布模块 使用规则

    jeecg 3.7.1  集群定时任务动态发布模块 使用规则   新版特性:    支持集群定时任务,支持分布式. 菜单路径: 系统监控-->定时任务 字段说明: 任务ID.任务说明:自定义即可 ...

  7. Springboot 项目启动后执行某些自定义代码

    Springboot 项目启动后执行某些自定义代码 Springboot给我们提供了两种"开机启动"某些方法的方式:ApplicationRunner和CommandLineRun ...

  8. SpringBoot 配置定时任务

    SpringBoot启用定时任务,其内部集成了成熟的框架,因此我们可以很简单的使用它. 开启定时任务 @SpringBootApplication //设置扫描的组件的包 @ComponentScan ...

  9. SpringBoot - 添加定时任务

    SpringBoot 添加定时任务 EXample1: import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spri ...

随机推荐

  1. python网络爬虫(4)结构与基本概念

    基本模型 请求与响应 import urllib.request as urllib2 request=urllib2.Request('http://www.zhihu.com') response ...

  2. js注册实现

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. Linux shellcode sample

    Linux shellcode sample HelloWorld.nasm ;HelloWorld.asm ;Author: Kul Subedi global _start section .te ...

  4. 总结 | 慢 SQL 问题经验总结

    1. 导致慢 SQL 的原因 在遇到慢 SQL 情况时,不能简单的把原因归结为 SQL 编写问题(虽然这是最常见的因素),实际上导致慢 SQL 有很多因素,甚至包括硬件和 mysql 本身的 bug. ...

  5. mysql复习(2)

    一.数据定义: SQL数据的定义包括模式的定义.表定义.视图定义和索引的定义. 1.基本的模式定义情况如下表. 2.一个关系数据库管理系统的实例中可以创建多个数据库,一个数据库中可以建立多个模式,一个 ...

  6. API接口之安全篇

    APP.前后端分离项目都采用API接口形式与服务器进行数据通信,传输的数据被偷窥.被抓包.被伪造时有发生,那么如何设计一套比较安全的API接口方案呢? 一般的解决方案如下: 1.Token授权认证,防 ...

  7. Swift(三)基本运算符

    Swift支持大部分标准C语言的运算符,并且对许多特性进行改进来减少常规编码的错误.除了支持基本运算符外,Swift还提供了2个特殊的运算符,分别是:溢出运算符和区间运算符 首先看下基本运算符 imp ...

  8. javaweb中的标签的核心标签库的常用标签

    //标签的使用使得页面的代码更加简洁,jsp脚本的尽可能少的使用,所以熟练掌握标签对于开发是很有必要的 <%--set设置数据,默认在page域 --%> <c:set var=&q ...

  9. xml_dom解析之二

    dom解析(二) 通过代码创建一个xml文件 package xml4; import java.io.File; import javax.xml.parsers.DocumentBuilder; ...

  10. 浙大数据结构课后习题 练习二 7-2 Reversing Linked List (25 分)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...