springboot+springTask

  1. @EnableScheduling:開啓對定時任務的支持(啓動器上),因爲SpringBoot整合SpringTask不需要額外導包

    @SpringBootApplication
    @EnableScheduling
    public class BosManagementApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(BosManagementApplication.class, args);
        }
    }
    
  2. 執行的方法上添加@Scheduled(cron = “*/6 * * * * ?”),標識方法何時被執行

    @Component
    public class SchedulerTask01 {
    
        private int count = 0;
        //每六秒鐘執行一次
        @Scheduled(cron = "*/6 * * * * ?")
        private void process() {
            System.out.println("定時任務1:" + (count++));
        }
    }
    
  3. @Scheduled參數的說明

    • 一種是我們常用的cron="*/6 * * * * ?"

    • 一種是 fixedRate = 6000,兩種都表示每隔六秒打印一下內容。

    • @Scheduled(fixedRate = 6000) :上一次開始執行時間點之後6秒再執行

    • @Scheduled(fixedDelay = 6000) :上一次執行完畢時間點之後6秒再執行

    • @Scheduled(initialDelay=1000, fixedRate=6000) :第一次延遲1秒後執行,之後按fixedRate的規則每6秒執行一次

  4. corn 表達式的生成
    鏈接: https://pan.baidu.com/s/1cY8ht0arSukiHCW76hRjiQ 提取碼: svqa

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章