1. import com.patient.core.adapter.CorsFilter;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.scheduling.annotation.EnableScheduling;
  8.  
  9. @EnableScheduling//开启定时任务注解
  10. @SpringBootApplication
  11. @MapperScan("com.patient.core.mapper")
  12. public class PatientSystemApplication {
  13.  
  14. public static void main(String[] args) {
  15. SpringApplication.run(PatientSystemApplication.class, args);
  16. }
  17.  
  18. @Bean
  19. public FilterRegistrationBean setFilter() {
  20. FilterRegistrationBean filterBean = new FilterRegistrationBean();
  21. filterBean.setFilter(new CorsFilter());
  22. filterBean.setName("CorsFilter");
  23. filterBean.addUrlPatterns("/*");
  24. return filterBean;
  25. }
  26. }

  

  1. import com.patient.core.service.UserService;
  2. import com.patient.core.util.DateUtils;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.scheduling.Trigger;
  8. import org.springframework.scheduling.TriggerContext;
  9. import org.springframework.scheduling.annotation.SchedulingConfigurer;
  10. import org.springframework.scheduling.config.ScheduledTaskRegistrar;
  11. import org.springframework.scheduling.support.CronTrigger;
  12.  
  13. import java.util.Date;
  14.  
  15. @Configuration
  16. public class ReminderTask implements SchedulingConfigurer {
  17. private static Logger log = LoggerFactory.getLogger(ReminderTask.class);
  18.  
  19. @Autowired
  20. private UserService userService;
  21.  
  22.   
    //项目启动会自动执行该Controller 类,首先执行触发器的方法,查询要执行业务逻辑的时间(需要转换成Cron表达式),动态的执行业务逻辑,业务逻辑执行完毕会再次执行触发器 设定下次执行时间
  23. @Override
  24. public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
  25. scheduledTaskRegistrar.addTriggerTask(doTask(), getTrigger());
  26. }
  27.  
  28. // 业务执行方法
  29. private Runnable doTask() {
  30. return new Runnable() {
  31. @Override
  32. public void run() {
  33. try {
  34. //业务逻辑
  35. log.info("业务执行方法");
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. };
  41. }
  42.  
  43. //业务触发器
  44. private Trigger getTrigger() {
  45. return new Trigger() {
  46. @Override
  47. public Date nextExecutionTime(TriggerContext triggerContext) {
  48. String dateStr = userService.getLastTime();
  49. if ("".equals(dateStr) || dateStr == null)
  50. return null;
  51. String cron = DateUtils.getCron(DateUtils.strToDateLong(dateStr));
  52. //定时任务触发,可修改定时任务的执行周期
  53. CronTrigger trigger = new CronTrigger(cron);
  54. Date nextExecDate = trigger.nextExecutionTime(triggerContext);
  55. log.info("业务触发器:"+DateUtils.dateToStrLong(nextExecDate));
  56. return nextExecDate;
  57. }
  58. };
  59. }

  

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. 类的函数成员之属性property

    属性命名采用Pascal命名方式,每个单词的首字母大写.访问方式与访问类的公共字段类似. /// <summary> /// 字段 /// </summary> private ...

  2. uoj #46[清华集训2014]玄学

    uoj 因为询问是关于一段连续区间内的操作的,所以对操作构建线段树,这里每个点维护若干个不交的区间,每个区间\((l,r,a,b)\)表示区间\([l,r]\)内的数要变成\(ax+b\) 每次把新操 ...

  3. websocket具体如何使用

    本人是在https://blog.csdn.net/jintingbo/article/details/80755636此地址学习的,所以留做笔记用于之后的学习 现在在写一个工程,是关于监控摄像头的, ...

  4. 微软内部封杀 Slack

    就在 Slack 在股市上亮相之际,有一家大公司却不允许员工在日常工作中使用这款企业协作和聊天应用软件. 微软已禁止其 100000 多名员工使用免费版 Slack.IT 外媒 GeekWire 报道 ...

  5. Rinetd 通过ECS端口转发到内网RDS

    前置条件 实现目的:开发本地电脑需要连接没有外网地址的RDS,通过ECS进行转发连接到RDS数据库 客户 PC 终端可以 ssh 登录有公网的 ECS 服务器. 有公网的 ECS 服务器可以通过内网访 ...

  6. linux pip使用国内源

    最近在Linux里面使用pip安装应用的速度十分的慢,于是便上网找了一些国内的源. 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:https:// ...

  7. Python测开面试题之装饰器

    Python的装饰器是面试常被问到的问题之一,在面试Python测试开发时被问到的概率不低于70%,那么装饰器的原理是什么,怎么快速写出一个装饰器呢,接下来我们详细讲解装饰器的实现方法. Python ...

  8. jmeter解析response里的json对象和数组

    1.解析提取json对象 2.解析提取json数组 注意,标红这里是从0开始计数 提取最后一个数组

  9. pidstat 命令(Linux 进程使用资源情况采样)

    pidstat 作用 pidstat 获取服务器指定进程的使用资源信息(包括 CPU.设备IO.内存.线程.任务切换等). 执行一波 [root@wille ~]# pidstat Linux 2.6 ...

  10. nicstat命令安装与分析

    nicstat安装包下载与安装: wget https://downloads.sourceforge.net/project/nicstat/nicstat-1.95.tar.gz tar -zxv ...