Spring-Boot中@Scheduled注解不生效

  今天测试来找我,说定时的策略任务不能运行了,或者有时候运行有时候不运行,很奇怪。之前都好好,百思不得其解。

  后来发现多了一个定时任务类,且都是用的@Scheduled注解。

  突然就恍然大悟,记得在哪里看到过,如果在多个函数上使用了@Scheduled,那么一定是一个执行完毕,才能排下一个。

  然后发现某些时间点我的任务会被阻塞。

  以下是两个定时任务类都用了这个注解的图。

  

  解决方法如下,配置线程池,创建一个类配置,如下:

import java.util.concurrent.ScheduledThreadPoolExecutor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ThredConfig {
	@Bean
	public ScheduledThreadPoolExecutor scheduledExecutorService() {
		ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
		return executor;
	}
}

  这就是我踩的坑,学习的路上任重而道远。如有错漏,欢迎指正。

猜你喜欢

转载自www.cnblogs.com/timeout/p/10478274.html