美文网首页
Schedule(定时任务)

Schedule(定时任务)

作者: Aeroball | 来源:发表于2020-01-03 23:06 被阅读0次

    虽然定时任务可以嵌⼊到web应⽤程序和WAR⽂件中,但下⾯演⽰⼀种更简单的⽅法创建了⼀个独⽴的应⽤程序。您将所有的内容打包在⼀个单⼀的、可执⾏的JAR⽂件中,⽤⼀个传统Java main()⽅法驱动。这也就是springboot的启动类。

    一、入门案例

    1.1 主程序
    package com.zqh;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    @SpringBootApplication
    @EnableScheduling
    public class VueApplication {
        public static void main(String[] args) {
            SpringApplication.run(VueApplication.class,args);
        }
    }
    
    1.2 任务类代码
    @Component
    public class MyScheduled {
        
        /**
         *  fixedRate: 固定时间去执行。
         *  fixedDelay: 固定的延迟。
         *
         *  cron有六个站位符号: 第一个表示秒,第二个是分,第三个小时,第四是日,
         *   第五个是月份,第六个是星期
         *   0/2 * * * * *  每两秒执行一次
         *   0 25 11 * * * 每天11:25执行
         *   0 0/5 11 * * *  每天的11点,每隔5分钟执行一次
         *   0 0 20 * * *  每天晚上8点钟执行
         *   0 0 8,20 * * * 每天早晚8点执行一次
         *   0 0 8-20 * * * 每天早上8点到晚8点,每个小时执行一次
         *   0 0 12 L * * * 每个月的最后一天12点钟执行。
         */
        @Scheduled(fixedRate = 5000)
        public void test1() {
            System.out.println("定时任务触发了");
        }
    }
    

    二、 参数介绍

    • <font color="red">fixedRate</font>:表示从调用开始每次延迟多少毫秒继续调用(如果有一个任务超时了,则等待此任务执行完成再执行下个任务)
    • <font color="red">fixedDelay</font>:表示一个任务结束后延迟多少秒继续下一个周期
    • <font color="red">initialDelay</font>:表示在第一次执行fixedRate()或fixedDelay()任务之前延迟的毫秒数
    //单位是毫秒,表⽰第⼀次执⾏fixedDelay()任务之前先延迟10秒
    @Scheduled(fixedDelay=5000, initialDelay=10000)
    
    • <font color="red">@Scheduled(cron="* * * * *")</font>:
    "0 0 * * * *" = 每天每时整点
    "*/10 * * * * *" = 每⼗秒(10:20:00, 10:20:10, 10:20:20 ...)触发
    "0 0 8-10 * * *" = 每天早上8:00、9:00 和 10:00 触发
    "0 0 6,19 * * *" = 每天6:00 和 19:00 触发
    "0 0/30 8-10 * * *" = 每天8:00, 8:30, 9:00, 9:30, 10:00 和 10:30 触发
    "0 0 9-17 * * MON-FRI" = 朝九晚五(周⼀⾄周五9:00-17:00的整点)触发
    "0 0 0 25 12 ?" = 圣诞节(每年的12⽉25⽇00:00)触发
    "0 15 10 L * ?" = 每⽉最后⼀⽇的上午10:15触发
    "0 15 10 ? * 6L" = 每⽉的最后⼀个星期五上午10:15触发
    "0 15 10 ? * 6#3" = 每⽉的第三个星期五上午10:15触发 1234567891011
    

    三、并行执行任务

      上面的方式中,如果我们开启多个定时任务,他们会使用同一个线程开启任务,可能会因为某个任务阻塞或异常而导致其他任务没有执行。所以我们可以使用多线程并行执行任务,只需要添加一个线程池即可。

    3.1 并行类
    @Component
    public class MySheduleConfig implements SchedulingConfigurer {
    
        public Executor getExecutor() {
            return Executors.newScheduledThreadPool(3); //开启特定的任务线程,开启3个
        }
    
        //
        @Override
        public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
            taskRegistrar.setScheduler(getExecutor());
        }
    }
    
    3.2 任务类
    @Component
    public class MySchedule {
    
        @Scheduled(cron = "0/5 * * * * *")
        public void doSomething() {
            System.out.println("每五秒执行的任务线程名:" + Thread.currentThread().getName());
            System.out.println("定时任务开始................");
        }
    
        @Scheduled(cron = "0/3 * * * * *")
        public void doSomething1() {
            System.out.println("每3秒执行的任务线程名:" + Thread.currentThread().getName());
            System.out.println("定时任务每3秒执行一次次,任务开始执行...");
        }
    
        @Scheduled(cron = "0/10 * * * * *")
        public void doSomething2() {
            System.out.println("每10秒执行的任务线程名:" + Thread.currentThread().getName());
            System.out.println("定时任务每天早8点到晚8点,每20分钟执行一次,开始");
        }
    }
    

    相关文章

      网友评论

          本文标题:Schedule(定时任务)

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