SpringBoot2.0系列--06--定时任务Scheduled及具体例子

╰+哭是因爲堅強的太久メ 2022-05-06 03:24 64阅读 0赞

SpringBoot2.0系列–06–定时任务Scheduled及具体例子

文章目录

  • SpringBoot2.0系列—06—定时任务Scheduled及具体例子
    • 前言
    • 介绍
    • 总流程
    • 时间循环参数
      • fixedRate
      • fixedDelay
      • cron
      • 文字解释
      • 代码解释
    • 示例代码
    • 联系方式

前言

JDK出11了,SpringBoot出2.0了,还没有系统的学习过,刚好最近项目中有使用到,就把一些关键的东西列出来,避免忘记
SpringBoot2.0系列–00–目录

介绍

SpringBoot的定时任务就是Spring里面的,可以直接使用Scheduled注解,

在这篇里面讲过这个类似的
https://blog.csdn.net/cmqwan/article/details/81117639

下面直接看下怎么使用吧

总流程

  1. 只要是交由Spring管理的类,都可以在其中的方法上面加上Scheduled注解,然后设置循环时间
  2. 在SpringBoot的主入口(Application)中添加@EnableScheduling注解,开启定时任务功能

时间循环参数

  1. fixedRate
  2. fixedDelay
  3. cron

fixedRate

不管内部执行过程,到时间就执行一次,单位毫秒

fixedDelay

在上一个执行完毕之后,执行下一个任务,单位毫秒

cron

这个参数可以从秒到年进行设置,扩展性最好,但是也最复杂

  1. cron是一个表达式 可以看下这篇https://www.cnblogs.com/ark-blog/p/9000079.html
  2. 字段名 含义          允许的值 允许的特殊字符
  3. seconds 0-59 , - * /
  4. minutes        0-59 , - * /
  5. hours 小时   0-23 , - * /
  6. daysOfMonth 1-31 , - * ? / L W C
  7. months        1-12 or JAN-DEC , - * /
  8. daysOfWeek 周几   1-7 or SUN-SAT , - * ? / L C #
  9. ---------------------
  10.   0 0 10,14,16 * * ? 每天上午10点,下午2点,4
  11.   0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
  12.   0 0 12 ? * WED 表示每个星期三中午12
  13.   "0 0 12 * * ?" 每天中午12点触发
  14.   "0 15 10 ? * *" 每天上午10:15触发
  15.   "0 15 10 * * ?" 每天上午10:15触发
  16.   "0 15 10 * * ? *" 每天上午10:15触发
  17.   "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
  18.   "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
  19.   "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
  20.   "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
  21.   "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:102:44触发
  22.   "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
  23.   "0 15 10 15 * ?" 每月15日上午10:15触发
  24.   "0 15 10 L * ?" 每月最后一日的上午10:15触发
  25.   "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
  26.   "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

文字解释

  1. (T是任务,W是间隔时间,假设是5秒的间隔)
  2. fixedRate: 不管内部执行过程,到时间就执行一次
  3. T1.T1WWWT2.T2.T2WW.T3.T3.T3.T3.T3.T4.T4.T4.T4.T4.T4.T4T5T5WWWT6.T6........
  4. fixedDelay: 在上一个执行完毕之后,执行下一个任务
  5. T1.T1.WWWWW.T2.T2.T2WWWWW.T3.T3.T3.T3.T3.WWWWW.T4.T4.T4.T4.T4.T4.T4.WWWWWT6.T6......
  6. ---------------------

代码解释

大家可以看下下面的代码,执行下面的代码,打印出来的log。

代码中都是每3秒执行一次,在方法中sleep 1秒,然后打印出当前时间,整理好的log以图片的形式发出来。

可以看到在87秒的时间内fixedRate执行了30次,cron也是30次,fixedDelay是20次
在这里插入图片描述

  1. 2018-10-12 19:29:14.464 fixedRate 1 time 1539343754464
  2. 2018-10-12 19:29:15.464 fixedDelay 1 time 1539343755464
  3. 2018-10-12 19:29:16.465 cron 1 time 1539343756465
  4. 2018-10-12 19:29:17.465 fixedRate 2 time 1539343757465
  5. 2018-10-12 19:29:19.002 cron 2 time 1539343759002
  6. 2018-10-12 19:29:20.003 fixedDelay 2 time 1539343760003
  7. 2018-10-12 19:29:21.003 fixedRate 3 time 1539343761003
  8. 2018-10-12 19:29:22.003 cron 3 time 1539343762003
  9. 2018-10-12 19:29:23.465 fixedRate 4 time 1539343763465
  10. 2018-10-12 19:29:24.465 fixedDelay 3 time 1539343764465
  11. 2018-10-12 19:29:25.465 cron 4 time 1539343765465
  12. 2018-10-12 19:29:26.466 fixedRate 5 time 1539343766466
  13. 2018-10-12 19:29:28.003 cron 5 time 1539343768003
  14. 2018-10-12 19:29:29.003 fixedDelay 4 time 1539343769003
  15. 2018-10-12 19:29:30.003 fixedRate 6 time 1539343770003
  16. 2018-10-12 19:29:31.004 cron 6 time 1539343771004
  17. 2018-10-12 19:29:32.466 fixedRate 7 time 1539343772466
  18. 2018-10-12 19:29:33.467 fixedDelay 5 time 1539343773467
  19. 2018-10-12 19:29:34.468 cron 7 time 1539343774468
  20. 2018-10-12 19:29:35.468 fixedRate 8 time 1539343775468
  21. 2018-10-12 19:29:37.002 cron 8 time 1539343777002
  22. 2018-10-12 19:29:38.003 fixedDelay 6 time 1539343778003
  23. 2018-10-12 19:29:39.003 fixedRate 9 time 1539343779003
  24. 2018-10-12 19:29:40.003 cron 9 time 1539343780003
  25. 2018-10-12 19:29:41.465 fixedRate 10 time 1539343781465
  26. 2018-10-12 19:29:42.466 fixedDelay 7 time 1539343782466
  27. 2018-10-12 19:29:43.466 cron 10 time 1539343783466
  28. 2018-10-12 19:29:44.467 fixedRate 11 time 1539343784467
  29. 2018-10-12 19:29:46.003 cron 11 time 1539343786003
  30. 2018-10-12 19:29:47.003 fixedDelay 8 time 1539343787003
  31. 2018-10-12 19:29:48.003 fixedRate 12 time 1539343788003
  32. 2018-10-12 19:29:49.004 cron 12 time 1539343789004
  33. 2018-10-12 19:29:50.466 fixedRate 13 time 1539343790466
  34. 2018-10-12 19:29:51.466 fixedDelay 9 time 1539343791466
  35. 2018-10-12 19:29:52.467 cron 13 time 1539343792467
  36. 2018-10-12 19:29:53.467 fixedRate 14 time 1539343793467
  37. 2018-10-12 19:29:55.002 cron 14 time 1539343795002
  38. 2018-10-12 19:29:56.002 fixedDelay 10 time 1539343796002
  39. 2018-10-12 19:29:57.003 fixedRate 15 time 1539343797003
  40. 2018-10-12 19:29:58.003 cron 15 time 1539343798003
  41. 2018-10-12 19:29:59.464 fixedRate 16 time 1539343799464
  42. 2018-10-12 19:30:00.465 fixedDelay 11 time 1539343800465
  43. 2018-10-12 19:30:01.465 cron 16 time 1539343801465
  44. 2018-10-12 19:30:02.466 fixedRate 17 time 1539343802466
  45. 2018-10-12 19:30:04.003 cron 17 time 1539343804003
  46. 2018-10-12 19:30:05.003 fixedDelay 12 time 1539343805003
  47. 2018-10-12 19:30:06.003 fixedRate 18 time 1539343806003
  48. 2018-10-12 19:30:07.004 cron 18 time 1539343807004
  49. 2018-10-12 19:30:08.465 fixedRate 19 time 1539343808465
  50. 2018-10-12 19:30:09.465 fixedDelay 13 time 1539343809465
  51. 2018-10-12 19:30:10.466 cron 19 time 1539343810466
  52. 2018-10-12 19:30:11.466 fixedRate 20 time 1539343811466
  53. 2018-10-12 19:30:13.001 cron 20 time 1539343813001
  54. 2018-10-12 19:30:14.001 fixedDelay 14 time 1539343814001
  55. 2018-10-12 19:30:15.002 fixedRate 21 time 1539343815002
  56. 2018-10-12 19:30:16.002 cron 21 time 1539343816002
  57. 2018-10-12 19:30:17.464 fixedRate 22 time 1539343817464
  58. 2018-10-12 19:30:18.465 fixedDelay 15 time 1539343818465
  59. 2018-10-12 19:30:19.465 cron 22 time 1539343819465
  60. 2018-10-12 19:30:20.465 fixedRate 23 time 1539343820465
  61. 2018-10-12 19:30:22.002 cron 23 time 1539343822002
  62. 2018-10-12 19:30:23.003 fixedDelay 16 time 1539343823003
  63. 2018-10-12 19:30:24.003 fixedRate 24 time 1539343824003
  64. 2018-10-12 19:30:25.004 cron 24 time 1539343825004
  65. 2018-10-12 19:30:26.465 fixedRate 25 time 1539343826465
  66. 2018-10-12 19:30:27.465 fixedDelay 17 time 1539343827465
  67. 2018-10-12 19:30:28.465 cron 25 time 1539343828465
  68. 2018-10-12 19:30:29.466 fixedRate 26 time 1539343829466
  69. 2018-10-12 19:30:31.003 cron 26 time 1539343831003
  70. 2018-10-12 19:30:32.003 fixedDelay 18 time 1539343832003
  71. 2018-10-12 19:30:33.004 fixedRate 27 time 1539343833004
  72. 2018-10-12 19:30:34.004 cron 27 time 1539343834004
  73. 2018-10-12 19:30:35.464 fixedRate 28 time 1539343835464
  74. 2018-10-12 19:30:36.465 fixedDelay 19 time 1539343836465
  75. 2018-10-12 19:30:37.465 cron 28 time 1539343837465
  76. 2018-10-12 19:30:38.465 fixedRate 29 time 1539343838465
  77. 2018-10-12 19:30:40.002 cron 29 time 1539343840002
  78. 2018-10-12 19:30:41.003 fixedDelay 20 time 1539343841003
  79. 2018-10-12 19:30:42.003 fixedRate 30 time 1539343842003
  80. 2018-10-12 19:30:43.004 cron 30 time 1539343843004

示例代码

  1. 只要是交由Spring管理的类,都可以在其中的方法上面加上Scheduled注解,然后设置循环时间

    /*

    • Copyright (C), 2015-2018
    • FileName: MyScheduler
    • Author: zhao
    • Date: 2018/10/12 19:07
    • Description: 测试用的定时任务
    • History:
    • 作者姓名 修改时间 版本号 描述
      */
      package com.lizhaoblog.pro006scheduled.scheduled;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;

    /**

    • 〈一句话功能简述〉
    • 〈测试用的定时任务〉
      *
    • @author zhao
    • @date 2018/10/12 19:07
    • @since 1.0.1
      */
      @Component
      public class MyScheduler {

      private static final Logger logger = LoggerFactory.getLogger(MyScheduler.class);

      private int count = 0;
      private int count2 = 0;
      private int count3 = 0;

      @Scheduled(fixedRate = 3000)
      public void fixedRate() {
      try {
      Thread.sleep(1000);
      } catch (InterruptedException e) {
      logger.debug(“fixedRate”, e);
      }
      count++;
      logger.info(“fixedRate “ + count + “ time “ + System.currentTimeMillis());
      }

      @Scheduled(fixedDelay = 3000)
      public void fixedDelay() {
      try {
      Thread.sleep(1000);
      } catch (InterruptedException e) {
      logger.debug(“fixedDelay”, e);
      }
      count2++;
      logger.info(“fixedDelay “ + count2 + “ time “ + System.currentTimeMillis());
      }

      // 每隔3秒执行一次:/3 ?
      @Scheduled(cron = “
      /3 ?”)
      public void cron() {
      try {
      Thread.sleep(1000);
      } catch (InterruptedException e) {
      logger.debug(“cron”, e);
      }
      count3++;
      logger.info(“cron “ + count3 + “ time “ + System.currentTimeMillis());
      }

    }

  2. 在SpringBoot的主入口(Application)中添加@EnableScheduling注解,开启定时任务功能

    package com.lizhaoblog.pro006scheduled;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableScheduling;

    @SpringBootApplication
    @EnableScheduling
    public class Pro006ScheduledApplication {

    public static void main(String[] args) {

    1. SpringApplication.run(Pro006ScheduledApplication.class, args);

    }
    }

联系方式

项目代码路径码云:https://gitee.com/lizhaoandroid/Springboot-Learning-lz

联系方式:QQ3060507060

查看下一篇或者其他文章,可点击目录或者专栏查看

发表评论

表情:
评论列表 (有 0 条评论,64人围观)

还没有评论,来说两句吧...

相关阅读