zoukankan      html  css  js  c++  java
  • Springboot的默认定时任务——Scheduled注解

    本博客参考博文地址

    1.pom依赖:

    引入springboot starter包即可

    <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>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    2.启动类启用定时任务:

    在启动类上加注解:@EnableScheduling即可实现。

    @SpringBootApplication
    @EnableScheduling
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

    3.创建定时任务实现类:

    定时任务1:

    @Component
    public class SchedulerTask {
    
        private int count=0;
    
        @Scheduled(cron="*/6 * * * * ?")
        private void process(){
            System.out.println("this is scheduler task runing  "+(count++));
        }
    
    }

    定时任务2:

    @Component
    public class Scheduler2Task {
    
        private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    
        @Scheduled(fixedRate = 6000)
        public void reportCurrentTime() {
            System.out.println("现在时间:" + dateFormat.format(new Date()));
        }
    
    }

    结果如下:

    this is scheduler task runing  0
    现在时间:09:44:17
    this is scheduler task runing  1
    现在时间:09:44:23
    this is scheduler task runing  2
    现在时间:09:44:29
    this is scheduler task runing  3
    现在时间:09:44:35

    参数说明:

      @Scheduled接受两种定时的设置:

        一种是cornexpression。

        一种是Rate/Delay表达式(毫秒值):

          @Scheduled(fixedRate = 6000):上一次开始执行时间点后每隔6秒执行一次。

          @Scheduled(fixedDelay = 6000):上一次执行完毕时间点之后6秒再执行。

          @Scheduled(initialDelay=1000, fixedRate=6000):第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次。

     

    cornexpression表达式详解:

      完整字段:[秒] [分] [小时] [日] [月] [周] [年]

      字段      允许值            允许特殊字符

       秒       0-59              , - * /

      分       0-59              , - * /

      小时      0-23              , - * /

      日       1-31              , - * ? / L W C

      月       1-12或JAN-DEC          , - * /

      周       1-7或SUN-SAT          , - *  ? / L C #

      年       留空或1970-2099         , - * /

     

    注:

      *表示所有值,在分钟里表示每一分钟触发。在小时,日期,月份等里面表示每一小时,每一日,每一月。

      ?表示不指定值。表示不关心当前位置设置的值。 比如不关心是周几,则周的位置填写?。  主要是由于日期跟周是有重复的所以两者必须有一者设置为?

      - 表示区间。小时设置为10-12表示10,11,12点均会触发。

      ,表示多个值。 小时设置成10,12表示10点和12点会触发。

      / 表示递增触发。 5/15表示从第5秒开始,每隔15秒触发。

      L 表示最后的意思。 日上表示最后一天。星期上表示星期六或7。 L前加数据,表示该数据的最后一个。

         星期上设置6L表示最后一个星期五。  6表示星期五

      W表示离指定日期最近的工作日触发。15W离该月15号最近的工作日触发。

      #表示每月的第几个周几。 6#3表示该月的第三个周五。

     

      示例:

        "0 0 12 * * ?" 每天中午12点触发 
        "0 15 10 ? * *" 每天上午10:15触发 
        "0 15 10 * * ?" 每天上午10:15触发 
        "0 15 10 * * ? *" 每天上午10:15触发 
        "0 15 10 * * ? 2005" 2005年的每天上午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 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 
        "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发 
        每天早上6点     0 6 * * *     每两个小时     0 */2 * * * 
        晚上11点到早上8点之间每两个小时,早上八点    0 23-7/2,8 * * * 
        每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点     0 11 4 * 1-3 
        1月1日早上4点     0 4 1 1 *

  • 相关阅读:
    c++命名规范与代码风格
    subline的多行游标快捷键
    selenium中的action
    Fiddler 教程(转)
    java.util.NoSuchElementException解决办法
    http协议中的URI
    深入研究java.lang.Runtime类(转)
    深入研究java.lang.Process类(转)
    java调用autoit3脚本
    AutoIT转
  • 原文地址:https://www.cnblogs.com/lilinzhiyu/p/8108538.html
Copyright © 2011-2022 走看看