Springboot 定時

1.在啓動類中加入@EnableScheduling來開啓定時任務。
2.創建實現定時任務的Service
@Component
public class Sender {
//用於測試先模擬snap發送queue-----------------
@Scheduled(fixedRate = 5000)
public void sendMsg(){
System.out.println("sendMsg" );
}
}

注:

@Scheduled(fixedRate = 5000)
    public void timerToZZP(){
        System.out.println("ZZP:" + new Random().nextLong() + new SimpleDateFormat("HH:mm:ss").format(new Date()));
    }

    @Scheduled(fixedDelay = 50000)
    public void timerToReportCount(){
        for (int i = 0; i < 10; i++){
            System.out.println("<================its" + i + "count===============>" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
        }
    }

    @Scheduled(initialDelay = 50000,fixedRate = 6000)
    public void timerToReport(){
        for (int i = 0; i < 10; i++){
            System.out.println("<================delay :" + i + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "count===============>");
        }
    }

1.fixedRate:上一次 啓動時間點之後 X秒執行一次
2.fixedDelay:上一次 結束時間點之後 每X秒執行一次
3.initialDelay:第一次延遲 X秒執行,之後按照fixedRate的規則每X秒執行

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