1,静态定时任务

         1,创建静态定时任务类,位置放在启动类同级目录或者子包下面,方便扫描到定时任务类上的注解。

 2,静态定时任务类需要三个注解:@Configuration,标记该类为配置类
                @EnableScheduling,标记类为定时任务类,@Scheduled(cron="0/30 * * * * ?"),//标记方法为定时任务,cron表达式:                  (0/30)从0秒开始,每隔30秒执行一次

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;/*** springboot设置静态的定时任务**/
//标记配置类
@Configuration
//标记该类开启定时任务
@EnableScheduling
public class ScheduleTest {//标记方法为定时任务//cron表达式:(0/30)从0秒开始,每隔30秒执行一次@Scheduled(cron="0/30 * * * * ?")public void show (){System.out.println("简单静态的定时任务!");}
}

2,动态的定时任务

             1,动态定时任务类需要两个注解和实现一个接口

                   @Configuration 和 @EnableScheduling

                    实现接口:SchedulingConfigurer  // 接口的实现方法有多种,这里正式其中一种

2,动态定时任务类:

import com.example.top.demo.configure.ScheduleConfigure;
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.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;import java.util.Date;@Configuration
@EnableScheduling
public  class DymicSchedule  implements SchedulingConfigurer {/*** 定时任务配置类*/@Autowiredprivate ScheduleConfigure scheduleConfigure;/*** 需要执行的任务函数*/protected  void processTask(){// 是否执行定时任务if(Boolean.valueOf(scheduleConfigure.getIsSwitch())){System.out.println("开始执行动态定时任务!");}}@Overridepublic void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {// 添加触发器任务scheduledTaskRegistrar.addTriggerTask(new Runnable() {@Overridepublic void run() {processTask();}}, new Trigger() {@Overridepublic Date nextExecutionTime(TriggerContext triggerContext) {// 任务触发,可修改任务的执行周期CronTrigger trigger = new CronTrigger(scheduleConfigure.getCrom());Date nextExec = trigger.nextExecutionTime(triggerContext);return nextExec;}});}}

3,动态定时任务类:ScheduleConfigure

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;@Configuration
public class ScheduleConfigure {@Value("${task.switch}")private  String isSwitch ;@Value("${task.cron}")private  String crom ;public String getIsSwitch() {return isSwitch;}public void setIsSwitch(String isSwitch) {this.isSwitch = isSwitch;}public String getCrom() {return crom;}public void setCrom(String crom) {this.crom = crom;}
}

4,相关yml文件配置

task:taskName: 第一个配置任务switch: true     #是否开启定时任务cron: "0/10 * * * * ?"  #定时任务执行周期,cron表达式

   总结:动态和静态定时任务的区别就是执行任务周期的cron表达式是在配置文件中还是代码中

SpingBoot中创建动态和静态定时任务相关推荐

  1. 如何创建systemd定时任务

    1. 如何创建一个定时任务,通过systemd系统 1. 如何创建一个定时任务,通过systemd系统 1.1. systemd中的timer 1.2. 自定义定时任务 1.2.1. 具体步骤 1.2 ...

  2. 在Linux中创建静态库.a和动态库.so

    转自:http://www.cnblogs.com/laojie4321/archive/2012/03/28/2421056.html 在Linux中创建静态库.a和动态库.so 我们通常把一些公用 ...

  3. django项目中使用crontab定时任务

    django项目中使用crontab定时任务 django-crontab 在ubuntu-18.04.2上的使用 需求:django项目中需添加定时任务,定时执行某个函数或者自定义的命令等) 使用步 ...

  4. navicat创建MySql定时任务详解

    目录 navicat创建MySql 定时任务详解 一.开起定时任务 二.通过navicat创建定时任务 三.关闭.启动.别名.移动.删除event 四.查询Event信息 navicat创建MySql ...

  5. 定时任务:创建静态定时任务、动态定时任务

    定时任务有三种实现 1 jdk自带的定时任务 2 Quartz插件实现的定时任务,需要引入额外的包 3 SpringTask定时调度,是对jdk的再一次封装,不用引入其他包了,用spring的包就自带 ...

  6. navicat创建mysql定时任务_navicat创建MySql定时任务的方法详解

    navicat创建MySql 定时任务详解 事件(event)是MySQL在相应的时刻调用的过程式数据库对象.一个事件可调用一次,也可周期性的启动,它由一个特定的线程来管理的,也就是所谓的" ...

  7. swift 自定义滑动视图_在Swift中创建一个向上滑动菜单视图(以编程方式)

    swift 自定义滑动视图 This is a quick tutorial on how to create a slide-up menu view in iOS 这是有关如何在iOS中创建向上滑 ...

  8. navicat mysql 计划任务_navicat创建MySql定时任务的方法详解

    navicat创建MySql 定时任务详解 事件(event)是MySQL在相应的时刻调用的过程式数据库对象.一个事件可调用一次,也可周期性的启动,它由一个特定的线程来管理的,也就是所谓的" ...

  9. linux创建数据库Oracle用户,linux下ORACLE数据库中创建新用户

     http://www.cnblogs.com/cloudwalf/archive/2008/09/04/1284033.html http://www.aiisen.com/oracle-lin ...

最新文章

  1. pandas将dataframe数据列中的年、月、日列组合成单一的日期数据列实战
  2. 使用 ExtJs Extender Controls 遇到的第一个错误
  3. 页面实现文字滚动效果(跑马灯)
  4. 博士申请 | 香港理工大学滕龙老师课题组招收机器人方向博士生/研究助理
  5. NSubstitute完全手册(一)入门基础
  6. matlab207a,MATLAB教程R2012a课后习题答案
  7. js reduce实现中间件_js数组高阶方法reduce经典用法代码分享
  8. oracle impdp导入时卡住,Oracle:impdp导入等待statement suspended, wait error to be cleared
  9. 接口测试基础——第5篇xlrd模块
  10. python运行input不出结果_Python中print和input调用了Python中底层的什么方法
  11. 微表情如何用计算机分析计算,面部微表情识别若干关键技术之计算机研究
  12. python手机安装不了软件怎么办_安装python安装方法
  13. 拓端tecdat|R语言用回归构建配对交易(Pairs Trading)策略量化模型分析股票收益和价格
  14. Windows压力测试工具SuperBenchmarker
  15. 【深度学习之美笔记】人工“碳”索意犹尽,智能“硅”来未可知(入门系列之二)
  16. 灰灰考研c语言讲义,【灰灰考研】操作系统复习全书.pdf
  17. python:实现Lempel-Ziv压缩算法(附完整源码)
  18. 网页中视频内容自动播放
  19. linux usb 网络摄像头,树莓派|Linux有问必答:如何在树莓派上安装USB网络摄像头...
  20. 2019年9月8日秋季PAT甲级题解A1163(7-4)Dijkstra Sequence

热门文章

  1. 萌萌媛の【剑指offer笔记】二维数组中的查找
  2. ArcGIS中如何从中国行政边界矢量数据中获取部分省市区县乡的矢量数据
  3. 基本求导法则与求导公式
  4. 全角空格和半角空格的运用
  5. 大学物理 电容的充放电
  6. 4399前端面试总结
  7. c语言对字符串进行切割strsep
  8. 基于MATLAB控制系统辨识系列2-最小二乘法
  9. CAXA CAPP工艺图表2020中文版
  10. HUST 1584 摆放餐桌(计算几何)