Scheduled定時任務
阿新 • • 發佈:2021-06-25
說明:SpringBoot使用@Scheduled建立定時任務
package com.lch.task; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date; //開啟定時任務(也可以放在啟動類上) @EnableScheduling //例項化定時任務 @Component //主要用於標記配置類,兼備Component的效果 //@Configuration public class ScheduledTask { //設定執行時間 @Scheduled(cron = "* * * * * *") public void test() { System.out.println("執行定時任務:" + new Date()); } }
@Scheduled中的引數
@Scheduled(fixedRate=3000):上一次開始執行時間點後3秒再次執行;
@Scheduled(fixedDelay=3000):上一次執行完畢時間點後3秒再次執行;
@Scheduled(initialDelay=1000, fixedDelay=3000):第一次延遲1秒執行,然後在上一次執行完畢時間點後3秒再次執行;
Cron表示式
cron = "* * * * * * *"
說明:cron從左到右(用空格隔開):秒 分 小時 月份中的日期 月份 星期中的日期 年份