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