• SpringBoot系列之使用Spring Task实现定时任务


    @

    一、前言介绍

    定时任务是企业开发中很常用的,比如定时推送一些接口数据,在java中实现定时任务的方法有Spring Task、Quartz等等框架,也有JDK自带的ScheduledExecutorService、Timer

    Quartz框架比较复杂,之前我写过一个入门教程,读者可以参考学习:Quartz系列之任务调度框架原理简介

    Spring Task是Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多

    二、Spring Task

    2.1 SpringTask简介

    Spring Task不是独立的项目,是spring-context 模块下提供的定时任务工具,是Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz

    2.2 实验环境准备

    • JDK 1.8
    • SpringBoot2.2.1
    • Maven 3.2+
    • 开发工具
      • IntelliJ IDEA
      • smartGit

    创建一个SpringBoot Initialize项目,详情可以参考我之前博客:SpringBoot系列之快速创建项目教程

    SpringBoot项目引入spring-boot-starter-web既可,因为wen场景启动器会自动引入spring-context依赖:
    在这里插入图片描述
    pom.xml参考

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example.springboot</groupId>
        <artifactId>springboot-scheduler-task</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>springboot-scheduler-task</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    

    2.3 Enable Scheduling

    开启Spring Scheduling必须使用@EnableScheduling注解,可以新建一个配置类,然后加上注解

    package com.example.springboot.scheduler.configuration;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    /**
     * <pre>
     *      TaskSchedulerConfiguration
     * </pre>
     *
     * <pre>
     * @author mazq
     * 修改记录
     *    修改后版本:     修改人:  修改日期: 2020/07/20 13:57  修改内容:
     * </pre>
     */
    @Configuration
    @EnableScheduling
    public class TaskSchedulerConfiguration {
    
    }
    
    

    也可以像官网例子一样,加在application类:
    在这里插入图片描述

    2.4 单线程定时任务

    Spring Task使用定时任务,只要加上@Scheduled注解,然后也要加到Spring容器中,使用可以加上@Service等注解就可以,Scheduled策略:cron 、fixedDelay、fixedRate 三选一

    ok,下面介绍@Scheduled的4个关键属性

    意思是:在上一次调用的结束与下一次调用的开始之间以固定的毫秒数为单位执行带注释的方法。

    ps:这种策略比较好理解,意思就是不管任务执行时间,只关注时间间隔就可以,画图表示:
    在这里插入图片描述

    • fixedRate

    在这里插入图片描述

    意思是: 两次调用之间以固定的时间段(以毫秒为单位)执行带注释的方法。

    这种策略先预计任务执行时间,fixedRate参数指定,画图描述,如图,fixedRate=10000(10s):
    在这里插入图片描述
    代码例子:每个任务计划执行5s

    @Scheduled( fixedRate = 5000)
        public void testFixedRate() {
            log.info("fixedRate test,thread name:[{}],execute time:[{}]",Thread.currentThread().getName(),
                    LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
    
    • cron
      cron参数是用cron表达式的意思,cron表达式含义:

    Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义。

    用表格表示Cron表达式:

    位置 时间域 允许值 特殊值
    1 0-59 ,- * /
    2 分钟 0-59 ,- * /
    3 小时 0-23 ,- * /
    4 日期 1-31 ,- * ? / L W C
    5 月份 1-12 ,- * /
    6 星期 1-7 ,- * ? / L C #
    7 年份(可选) 1-31 ,- * /
    特殊字符 代表含义
    , 枚举,表达一个列表值,如在星期字段中使用“MON,WED,FRI”,则表示星期一,星期三和星期五;
    - 区间 ,表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12;
    * 任意,可用在所有字段中,表示对应时间域的每一个时刻,例如, 在分钟字段时,表示“每分钟”;
    / 步长,x/y表达一个等步长序列,x为起始值,y为增量步长值。如在分钟字段中使用0/15,则表示为0,15,30和45秒,而5/15在分钟字段中表示5,20,35,50,你也可以使用*/y,它等同于0/y;
    ? 该字符只在日期和星期字段中使用,它通常指定为“无意义的值”,相当于点位符;
    L 该字符只在日期和星期字段中使用,代表“Last”的意思,但它在两个字段中意思不同。L在日期字段中,表示这个月份的最后一天,如一月的31号,非闰年二月的28号;如果L用在星期中,则表示星期六,等同于7。但是,如果L出现在星期字段里,而且在前面有一个数值X,则表示“这个月的最后X天”,例如,6L表示该月的最后星期五;
    W 工作日,该字符只能出现在日期字段里,是对前导日期的修饰,表示离该日期最近的工作日。例如15W表示离该月15号最近的工作日,如果该月15号是星期六,则匹配14号星期五;如果15日是星期日,则匹配16号星期一;如果15号是星期二,那结果就是15号星期二。但必须注意关联的匹配日期不能够跨月,如你指定1W,如果1号是星期六,结果匹配的是3号星期一,而非上个月最后的那天。W字符串只能指定单一日期,而不能指定日期范围;
    C 该字符只在日期和星期字段中使用,代表“Calendar”的意思。它的意思是计划所关联的日期,如果日期没有被关联,则相当于日历中所有日期。例如5C在日期字段中就相当于日历5日以后的第一天。1C在星期字段中相当于星期日后的第一天。
    # 该字符只能在星期字段中使用,表示当月某个工作日。如6#3表示当月的第三个星期五(6表示星期五,#3表示当前的第三个),而4#5表示当月的第五个星期三,假设当月没有第五个星期三,忽略不触发;
    LW LW组合,在日期字段可以组合使用LW,它的意思是当月的最后一个工作日;

    每隔1分钟执行一次:

    @Scheduled(cron = "0 0/1 * * * ? ")
        public void testCron(){
            log.info("cron test,thread name:[{}],execute time:[{}]",Thread.currentThread().getName(),
                    LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        
    
    • initialDelay
      initialDelay 初始化延迟时间,也就是标识第一次延迟执行的时间,只能配合 fixedDelay 或 fixedRate 使用
    @Scheduled(initialDelay = 5000, fixedRate = 5000)
        public void testFixedRate() {
            log.info("fixedRate test,thread name:[{}],execute time:[{}]",Thread.currentThread().getName(),
                    LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
    

    2.5 线程池的使用配置

    配置文件加上代码,配置线程池

    @Bean
        public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
            ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
            threadPoolTaskScheduler.setPoolSize(5);
            threadPoolTaskScheduler.setThreadNamePrefix("ThreadPoolTaskScheduler");
            threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
            threadPoolTaskScheduler.setAwaitTerminationSeconds(60);
            return threadPoolTaskScheduler;
        }
    

    也可以在application配置文件,加上:
    在这里插入图片描述
    ps:当然,不做配置也是可以的,因为SpringBoot有做自动配置,自己不改配置的,就全部按照SpringBoot的自动配置(默认)

    代码例子下载:code download

  • 相关阅读:
    白书数据结构基础总结
    UVA 10557 XYZZY 结题报告
    UVA 10047 The Monocycle 解题报告
    二叉查找树及其C语言实现
    堆排序及其c语言实现
    约瑟夫环问题小结
    KMP算法总结
    UVA 10129 Play on Words 解题报告
    深入浅出Node.js (8)
    洛谷 P1757 通天之分组背包
  • 原文地址:https://www.cnblogs.com/mzq123/p/13360748.html
Copyright © 2020-2023  润新知