美文网首页SpringBoot
SpringBoot 定时任务cron

SpringBoot 定时任务cron

作者: 程序员小杰 | 来源:发表于2020-06-21 21:45 被阅读0次

    一、静态:基于注解

    首先在启动类中加上注解@EnableScheduling

    @SpringBootApplication
    @EnableScheduling  //开启定时任务
    public class TasksApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(TasksApplication.class, args);
        }
    
    }
    

    二、

    @RestController
    @RequestMapping("/tasks/cron")
    public class CronController {
    
        //3.添加定时任务
        @Scheduled(cron = "0/5 * * * * ?")
        public void configureTasks() {
            System.err.println("执行定时任务: " + SimpleDateFormat.getDateTimeInstance().format(new Date()));
        }
    }
    
    image.png

    "0/5 * * * * ?"代表每隔五秒执行一次。

    cron表达式

    image.png

    cron一共有7位,最后一位是年,可以留空,所以我们可以写6位:
    第一位,表示秒,取值0-59
    第二位,表示分,取值0-59
    第三位,表示小时,取值0-23
    第四位,表示日,取值1-31
    第五位,表示月份,取值1-12
    第六位,表示星期/周,取值1-7,1表示星期天,2表示星期一。
    第7为,表示年份,可以留空,取值1970-2099
    其中最难理解的就是那些符号了,下面每个符号举个栗子:

    通用符号:,-*/

    ,:表示列出枚举值,例如在第二位使用5,35,表示在分钟数为5、35时执行。
    -:表示范围,例如在第二位使用5-35,表示在分钟数为5到35时每分钟都执行。
    *:表示匹配该域的任意值。例如在第二位使用*表示分钟数不做限制,每分钟都会执行。
    /:表示起始时间开始执行,然后每隔固定时间执行一次。例如在第二位使用5/6,表示在分钟数为5时执行一次,然后隔6分钟执行一次,也就是在11、17分钟再分别执行。

    专有符号

    ?:只能用在第四位(日)和第六位(星期)两个域,因为这两个域互斥,必须对其一设置?。
    L:表示最后。只能用在第四位(日)和第六位(星期)两个域,如果在第六位使用5L,意味着在最后的一个星期四执行。
    W:表示有效工作日(周一到周五),只能出现在第四位(日)域。系统将在离指定日期最近的有效工作日出发事件。如15W,表示最接近15号的工作日,可能是15号(刚好是工作日)那就在15号执行。15号如果不是工作日,是星期天,那就往后推,在16号执行。16号是最接近工作日的日期。
    LW:表示某个月最后一个工作日。
    #:用于确定每个月第几个星期几,只能出现在第六位(星期)域,例如4#3,表示某月的第3个星期三。
    C::只能用在第四位(日)和第六位(星期)两个域,需要关联日历,如果没关联可以忽略。

    image.png

    注意

    专有符号中除?外,在spirng定时任务中都不支持.
    测试

     @Scheduled(cron = "* * 20 ? * 1#3")
        public void configureTasks1() {
            System.err.println("执行定时任务: " + SimpleDateFormat.getDateTimeInstance().format(new Date()));
        }
    

    项目启动的时候就会出现异常

    Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'configureTasks1': For input string: "1#3"
    

    那在哪里能使用呢??
    Quartz支持。
    在线Cron表达式生成器可以去测试一下.

    image.png

    异步多线程实现

    定时任务默认是单线程的,如果认为持续时间较长,就会将后面的定时任务拖延,导致丢失任务。
    测试:

     @Scheduled(cron = "0/1 * * * * ?")
        public void configureTasks() throws InterruptedException {
            System.out.println("线程名111:" + Thread.currentThread().getName());
            Thread.sleep(2000);
            System.err.println("执行定时任务111: " + SimpleDateFormat.getDateTimeInstance().format(new Date()));
        }
    
        @Scheduled(cron = "0/1 * * * * ?")
        public void configureTasks2() throws InterruptedException {
            System.out.println("线程名2222:" + Thread.currentThread().getName());
    
            //Thread.sleep(1000);
            System.err.println("执行222: " + SimpleDateFormat.getDateTimeInstance().format(new Date()));
        }
    
    image.png

    可以看出这两个定时任务都是一个线程在执行,如果有一个定时任务执行比较耗时(比如:configureTasks模拟耗时,开启线程睡眠),就会造成任务丢失。

    1、开启异步注解

    启动类上加上注解

    @EnableAsync  
    

    2、设置异步执行

    @Scheduled(cron = "0/1 * * * * ?")
        @Async
        public void configureTasks() throws InterruptedException {
            System.out.println("线程名111:" + Thread.currentThread().getName() + "===="+ SimpleDateFormat.getDateTimeInstance().format(new Date()));
            Thread.sleep(2000);
    
        }
        @Scheduled(cron = "0/1 * * * * ?")
        @Async
        public void configureTasks2() throws InterruptedException {
            System.out.println("线程名2222:" + Thread.currentThread().getName() + "====" + SimpleDateFormat.getDateTimeInstance().format(new Date()));
            //Thread.sleep(1000);
        }
    
    image.png

    相关文章

      网友评论

        本文标题:SpringBoot 定时任务cron

        本文链接:https://www.haomeiwen.com/subject/vzoaxktx.html