• springboot内嵌定时任务使用及cron表达式讲解


    第一步:pom引入依赖

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>

    第二步:配置定时器

    package com.macro.mall.tiny.component;

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

    /**
    * 订单超时取消并解锁库存的定时器
    */
    @Component
    public class OrderTimeOutCancelTask {
    private Logger LOGGER = LoggerFactory.getLogger(OrderTimeOutCancelTask.class);

    /**
    * cron表达式:Seconds Minutes Hours DayofMonth Month DayofWeek [Year]
    * 每10分钟扫描一次,扫描设定超时时间之前下的订单,如果没支付则取消该订单
    */
    // cron配置一般是放在配置文件中
    // @Scheduled(cron = "0 0/10 * ? * ?")
    @Scheduled(cron = "${job.schedule}")
    private void cancelTimeOutOrder() {
    LOGGER.info("取消订单,并根据sku编号释放锁定库存");
    }
    }

    第三步:定时任务配置(可以直接在启动类上添加注解@EnableScheduling,也可以定义配置类,以下是配置类的形式)

    package com.macro.mall.tiny.config;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    /**
     * 定时任务配置
     */
    @Configuration
    @EnableScheduling
    public class SpringTaskConfig {
    }

    cron表达式讲解:

     /**
         * cron 知识讲解:
         * cron 每位的含义:
         *      - 第一位,表示秒,取值 0-59;
         *      - 第二位,表示分,取值 0-59;
         *      - 第三位,表示小时,取值 0-23;
         *      - 第四位,日期天/日,取值 1-31;
         *      - 第五位,日期月份,取值 1-12;
         *      - 第六位,星期,取值 1-7,星期一、星期二…;注:不是第1周、第2周的意思,另外:1表示星期天,2表示星期一。
         *      - 第七位,年份,可以留空,取值 1970-2099。
         *
         * cron 符号含义:
         *      (*)星号:可以理解为每的意思,每秒、每分、每天、每月、每年……。
         *      (?)问号:问号只能出现在日期和星期这两个位置,表示这个位置的值不确定,每天 12 点执行,所以第六位星期的位置是不需要关注的,就是不确定的值。同时,日期和星期是两个相互排斥的元素,通过问号来表明不指定值。
         *      (-)减号:表达一个范围,如在小时字段中使用“10-12”,则表示从 10~12 点,即 10、11、12。
         *      (,)逗号:表达一个列表值,如在星期字段中使用“1,2,4”,则表示星期天、星期一、星期三。
         *      (/)斜杠:如 x/y,x 是开始值,y 是步长,比如在第一位(秒) 0/15 就是,从 0 秒开始,每 15 秒,最后就是 0、15、30、45、60,另 * /y,等同于 0/y。
         *
         * 例子:
         *      0 0 1 * * ?     每天 1 点执行。
         *      0 5 1 * * ?     每天 1 点 5 分执行。
         *      0-5 * * * * ?   每分钟的0/1/2/3/4/5 秒执行
         *      0 5/10 1 * * ?  每天 1 点的 5 分、15 分、25 分、35 分、45 分、55 分这几个时间点执行。
         *      0 5 1 ? * 1     每周星期天,1点5分 执行,注:1 表示星期天。
         *      0 10 3 ? * 1#3  每个月的第 三 个星期,星期天执行,# 号只能出现在星期的位置。
         */
  • 相关阅读:
    SSH协议详解
    适用于Rick的奖惩体系
    LeetCode 4 Median of Two Sorted Array
    一丁点算法学习感悟
    algorithm ch7 QuickSort
    algorithm ch6 priority queque
    algorithm ch6 heapsort
    algorithm ch2 Merge_sort
    关于gsl库出现access violation 0X00000005问题的解决方法
    LeetCode 3 Longest Substring Without Repeating Characters
  • 原文地址:https://www.cnblogs.com/JohnsonLiu/p/11864501.html
Copyright © 2020-2023  润新知