【发布时间】:2018-07-29 11:38:25
【问题描述】:
我正在使用 Spring Boot,并配置了 ThreadPoolTaskExecutor 如下:
@Data
@Configuration
public class WorkflowThreadConfig {
@Value("${threadConfig.corePoolSize}")
private Integer corePoolSize;
@Value("${threadConfig.maxPoolSize}")
private Integer maxPoolSize;
@Bean
@Qualifier("threadPoolTaskExecutor")
public TaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
log.debug("threadPoolTaskExecutor maxPoolSize is : " + threadPoolTaskExecutor.getMaxPoolSize());
threadPoolTaskExecutor.setThreadNamePrefix("workflow_thread_");
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
}
当我使用 @Qualifier @Autowire 和 @Bean 进入另一个类时,我会看到最大池大小中的默认线程数,而不是我在配置中提供的线程数(10),即使在注释掉大多数之后也是如此我的代码,只使用@PostConstruct:
@Component
public class WorkflowTaskScheduler {
//@Autowired
//private WorkflowThreadManager workflowThreadManager;
@Autowired
@Qualifier("threadPoolTaskExecutor")
private TaskExecutor taskExecutor;
@PostConstruct
public void workflowTaskScheduler(){
ThreadPoolTaskExecutor threadPool = (ThreadPoolTaskExecutor) taskExecutor;
log.debug(" Max Thread Pool count is : " + threadPool.getMaxPoolSize());
}
}
日志:
SpanId="">threadPoolTaskExecutor maxPoolSize is : 10</L_MSG>
SpanId=""> Max Thread Pool count is : 2147483647</L_MSG>
另一个有趣的点是当我从threadPoolTaskExecutor @Bean 和@Autowired TaskExecutor 中删除@Qualifier 注释时,我收到以下错误:
Field taskExecutor in com.package.WorkflowTaskScheduler required a single bean, but 2 were found:
- threadPoolTaskExecutor: defined by method 'threadPoolTaskExecutor' in class path resource [com/package/WorkflowThreadConfig.class]
- taskScheduler: defined in null
【问题讨论】:
-
在 bean 声明期间不要使用
@Qualifier,而是使用@Primary。像这样@Bean(name = "threadPoolTaskExecutor") @Primary public TaskExecutor threadPoolTaskExecutor()并将@Qualifier 保留在您的 WorkflowTaskScheduler 类中 -
在这里查看我的答案。这是同一个问题stackoverflow.com/questions/48715714/…
-
@pvpkiran,我在使用
@Primary @Bean(name = "threadPoolTaskExecutor") public TaskExecutor threadPoolTaskExecutor() {时仍然遇到同样的问题 -
你在 WorkflowTaskScheduler 类中使用
@Qualifier吗? -
是的,我正在使用
@Qualifier("threadPoolTaskExecutor")
标签: spring dependency-injection threadpoolexecutor