Springboot定時任務


1、在啟動類上面加注解:

@EnableScheduling

2、定義一個bean,在方法上面加@Scheduled:

package cn.mmweikt.es.schedule;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class MyTask {
    @Scheduled(cron = "*/5 * * * * *") //5秒執行1次
    public void excute() {
        System.out.println("excute");
    }
}

啟動項目可見控制台輸出:

2019-01-24 23:04:56.815  INFO 4536 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-01-24 23:04:57.288  INFO 4536 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'

然后控制台每5秒打印excute,定時任務成功。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM