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. oa_mvc_easyui_项目搭建及登录页面验证码(1)

    1.空项目的搭建,三层的搭建(各层之中的引用) webapp:bll,model,common bll:dal,model dal:model 2.SQL表 ItcastDb:T_UserInfo,T ...

  2. C#中的编译为什么不叫Compile而叫build

    是因为Build包含了compile,build既compile了你自己的代码,又把你compile的Assembly和引用别人的Assembly放在一起buiid.

  3. CPU如何区分溢出和自然进位?

    CPU如何区分溢出和自然进位? 之前学习补码的时候倒是学会了基本概念,但是最近又接触时发现还有不清楚的地方,所以又研究了下 今天的核心问题的"CPU是如何区分高位自然舍弃和溢出的?" ...

  4. 误删除/dec/zero,/dev/null

    误删除/dev/zero [root@MYSQL-MONGO145 dev]# mknod /dev/zero c 1 5[root@MYSQL-MONGO145 dev]# chmod 666 /d ...

  5. 简单易用的字符串模糊匹配库Fuzzywuzzy

    简单易用的字符串模糊匹配库Fuzzywuzzy 阅读目录 FuzzyWuzzy 简介 安装 用法 已知移植 FuzzyWuzzy 简介 FuzzyWuzzy 是一个简单易用的模糊字符串匹配工具包.它依 ...

  6. CDH5.16.1升级kafka0.10到1.0.1

        激活1.0.1的包    

  7. hadoop工作流程

    一)任务流程 1)Mapreduce程序启动一个Jobclient实例,开启整个mapreduce作业 2)Jobclient通过getnewjobld()j接口向Jobtarker发出请求,以获得一 ...

  8. Linux: df du

    df :列出文件系统的整体磁盘使用量 du :评估文件系统的磁盘使用量(常用在推估目录所占容量) 1 df  :[-ahikHTm] 目录或文件名 选项或参数: -a:  列出所有的文件系统,包括系统 ...

  9. 牛客练习赛44 B 小y的线段 (思维)

    链接:https://ac.nowcoder.com/acm/contest/634/B 来源:牛客网 题目描述 给出n条线段,第i条线段的长度为a_ia i ​ ,每次可以从第i条线段的j位置跳到第 ...

  10. C语言的函数声明,定义,调用以及exit退出

    #include <stdio.h> #include <stdlib.h> void singing(); int main() { singing(); system(&q ...