工作中竟讓會用到定時任務,比如每日零點執行任務,清空前一天的無效數據。
SpringBoot中的@EnableScheduling 用於開啓定時任務
@Scheduled:控制執行時間
@Configuration //主要用於標記配置類,兼備Component的效果。
@EnableScheduling //開啓定時任務
public class WaybillNumTask {
private static int OrderNum = 0;
/**
每天零點執行一次
**/
@Scheduled(cron = "0 0 0 */1 * ?")
public void cleanCount(){
OrderNum = 0;
System.err.println("執行運單順序號歸零時間: " + LocalDateTime.now() +"OrderNum:"+ OrderNum);
}
@Scheduled註解:
使一個方法定時被執行的註解。其屬性cron/fixedDelay/fixedRate必須有一個被指定
該註解標記的方法沒有參數,也沒有返回值。即使寫了返回值,也會被忽略。