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,定时任务成功。