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

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

前言

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

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

     cron是一个表达式 可以看下这篇https://www.cnblogs.com/ark-blog/p/9000079.html
                        字段名              含义          允许的值                        允许的特殊字符  
                        seconds            秒                    0-59                            , - * /  
                        minutes        分                    0-59                            , - * /  
                        hours              小时                  0-23                            , - * /  
                        daysOfMonth        日                    1-31                            , - * ? / L W C  
                        months         月                    1-12 or JAN-DEC                 , - * /  
                        daysOfWeek         周几                  1-7 or SUN-SAT                  , - * ? / L C #
--------------------- 

  0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 
  0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时 
  0 0 12 ? * WED 表示每个星期三中午12点 
  "0 0 12 * * ?" 每天中午12点触发 
  "0 15 10 ? * *" 每天上午10:15触发 
  "0 15 10 * * ?" 每天上午10:15触发 
  "0 15 10 * * ? *" 每天上午10:15触发  
  "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发 
  "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 
  "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 
  "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 
  "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发 
  "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发 
  "0 15 10 15 * ?" 每月15日上午10:15触发 
  "0 15 10 L * ?" 每月最后一日的上午10:15触发 
  "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
  "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

文字解释

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

代码解释

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

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

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

2018-10-12 19:29:14.464   fixedRate 1 time 1539343754464
2018-10-12 19:29:15.464   fixedDelay 1 time 1539343755464
2018-10-12 19:29:16.465   cron 1 time 1539343756465
2018-10-12 19:29:17.465   fixedRate 2 time 1539343757465
2018-10-12 19:29:19.002   cron 2 time 1539343759002
2018-10-12 19:29:20.003   fixedDelay 2 time 1539343760003
2018-10-12 19:29:21.003   fixedRate 3 time 1539343761003
2018-10-12 19:29:22.003   cron 3 time 1539343762003
2018-10-12 19:29:23.465   fixedRate 4 time 1539343763465
2018-10-12 19:29:24.465   fixedDelay 3 time 1539343764465
2018-10-12 19:29:25.465   cron 4 time 1539343765465
2018-10-12 19:29:26.466   fixedRate 5 time 1539343766466
2018-10-12 19:29:28.003   cron 5 time 1539343768003
2018-10-12 19:29:29.003   fixedDelay 4 time 1539343769003
2018-10-12 19:29:30.003   fixedRate 6 time 1539343770003
2018-10-12 19:29:31.004   cron 6 time 1539343771004
2018-10-12 19:29:32.466   fixedRate 7 time 1539343772466
2018-10-12 19:29:33.467   fixedDelay 5 time 1539343773467
2018-10-12 19:29:34.468   cron 7 time 1539343774468
2018-10-12 19:29:35.468   fixedRate 8 time 1539343775468
2018-10-12 19:29:37.002   cron 8 time 1539343777002
2018-10-12 19:29:38.003   fixedDelay 6 time 1539343778003
2018-10-12 19:29:39.003   fixedRate 9 time 1539343779003
2018-10-12 19:29:40.003   cron 9 time 1539343780003
2018-10-12 19:29:41.465   fixedRate 10 time 1539343781465
2018-10-12 19:29:42.466   fixedDelay 7 time 1539343782466
2018-10-12 19:29:43.466   cron 10 time 1539343783466
2018-10-12 19:29:44.467   fixedRate 11 time 1539343784467
2018-10-12 19:29:46.003   cron 11 time 1539343786003
2018-10-12 19:29:47.003   fixedDelay 8 time 1539343787003
2018-10-12 19:29:48.003   fixedRate 12 time 1539343788003
2018-10-12 19:29:49.004   cron 12 time 1539343789004
2018-10-12 19:29:50.466   fixedRate 13 time 1539343790466
2018-10-12 19:29:51.466   fixedDelay 9 time 1539343791466
2018-10-12 19:29:52.467   cron 13 time 1539343792467
2018-10-12 19:29:53.467   fixedRate 14 time 1539343793467
2018-10-12 19:29:55.002   cron 14 time 1539343795002
2018-10-12 19:29:56.002   fixedDelay 10 time 1539343796002
2018-10-12 19:29:57.003   fixedRate 15 time 1539343797003
2018-10-12 19:29:58.003   cron 15 time 1539343798003
2018-10-12 19:29:59.464   fixedRate 16 time 1539343799464
2018-10-12 19:30:00.465   fixedDelay 11 time 1539343800465
2018-10-12 19:30:01.465   cron 16 time 1539343801465
2018-10-12 19:30:02.466   fixedRate 17 time 1539343802466
2018-10-12 19:30:04.003   cron 17 time 1539343804003
2018-10-12 19:30:05.003   fixedDelay 12 time 1539343805003
2018-10-12 19:30:06.003   fixedRate 18 time 1539343806003
2018-10-12 19:30:07.004   cron 18 time 1539343807004
2018-10-12 19:30:08.465   fixedRate 19 time 1539343808465
2018-10-12 19:30:09.465   fixedDelay 13 time 1539343809465
2018-10-12 19:30:10.466   cron 19 time 1539343810466
2018-10-12 19:30:11.466   fixedRate 20 time 1539343811466
2018-10-12 19:30:13.001   cron 20 time 1539343813001
2018-10-12 19:30:14.001   fixedDelay 14 time 1539343814001
2018-10-12 19:30:15.002   fixedRate 21 time 1539343815002
2018-10-12 19:30:16.002   cron 21 time 1539343816002
2018-10-12 19:30:17.464   fixedRate 22 time 1539343817464
2018-10-12 19:30:18.465   fixedDelay 15 time 1539343818465
2018-10-12 19:30:19.465   cron 22 time 1539343819465
2018-10-12 19:30:20.465   fixedRate 23 time 1539343820465
2018-10-12 19:30:22.002   cron 23 time 1539343822002
2018-10-12 19:30:23.003   fixedDelay 16 time 1539343823003
2018-10-12 19:30:24.003   fixedRate 24 time 1539343824003
2018-10-12 19:30:25.004   cron 24 time 1539343825004
2018-10-12 19:30:26.465   fixedRate 25 time 1539343826465
2018-10-12 19:30:27.465   fixedDelay 17 time 1539343827465
2018-10-12 19:30:28.465   cron 25 time 1539343828465
2018-10-12 19:30:29.466   fixedRate 26 time 1539343829466
2018-10-12 19:30:31.003   cron 26 time 1539343831003
2018-10-12 19:30:32.003   fixedDelay 18 time 1539343832003
2018-10-12 19:30:33.004   fixedRate 27 time 1539343833004
2018-10-12 19:30:34.004   cron 27 time 1539343834004
2018-10-12 19:30:35.464   fixedRate 28 time 1539343835464
2018-10-12 19:30:36.465   fixedDelay 19 time 1539343836465
2018-10-12 19:30:37.465   cron 28 time 1539343837465
2018-10-12 19:30:38.465   fixedRate 29 time 1539343838465
2018-10-12 19:30:40.002   cron 29 time 1539343840002
2018-10-12 19:30:41.003   fixedDelay 20 time 1539343841003
2018-10-12 19:30:42.003   fixedRate 30 time 1539343842003
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:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaoblog.pro006scheduled.scheduled;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈测试用的定时任务〉
 *
 * @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());
  }

}

  1. 在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) {
    SpringApplication.run(Pro006ScheduledApplication.class, args);
  }
}

联系方式

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

联系方式:QQ3060507060

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值