第十四章:springboot 定時任務

第一步工具

 啓動類裏面 @EnableScheduling開啓定時任務,自動掃描網站

第二步spa

編寫定時任務類 , 例如 .net

@Component
public class MySchedule {


    @Scheduled(fixedRate = 5000)
    public void test1(){
        System.out.println("===每5秒執行==="+new Date().toLocaleString());
    }

    @Scheduled(cron = "0 */1 * * * *")
    public void test2(){
        System.out.println("===每分鐘執行==="+new Date().toLocaleString());
    }

}

一、註解 @Component 被容器掃描blog

二、@Scheduled(、、)  定時執行規則crontab

SpringBoot經常使用定時任務表達式配置字符串

一、cron 定時任務表達式 @Scheduled(cron="*/1 * * * * *") 表示每秒
            1)crontab 工具  https://tool.lu/crontab/
二、fixedRate: 定時多久執行一次(上一次開始執行時間點後xx秒再次執行;)
三、fixedDelay: 上一次執行結束時間點後xx秒再次執行
四、fixedDelayString:  字符串形式,能夠經過配置文件指定get

 

啓動項目,控制檯輸出打印定時任務信息class

 

若是不肯定本身的 cron 表達式是否正確 ,去這個網站  https://tool.lu/crontab/test

相關文章
相關標籤/搜索