如定时任务启动后无反应,检查以下几项是否都有
1、检查application类上是否有 @EnableScheduling 注解
@EnableScheduling @SpringBootApplication public class ClassApplication { public static void main(String[] args) { SpringApplication.run(ClassApplication.class, args); } }
2、检查是否配置对应的 @Bean
@Bean public TaskScheduler taskScheduler() { ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.setPoolSize(20); scheduler.initialize(); return scheduler; }
3、检查需定时的类上是否有 @Component 注解
4、检查需定时的方法上是否有 @Scheduled(cron = "* * * * * * *") 注解
@Component @Slf4j public class ReadLog { @Scheduled(cron = "0 */5 * * * *") public void restart() { log.info("=======================定时任务=============================="); }
备注:
cron = "* * * * * * *"
秒 分 时 日 月 周 年