Spring ThreadPoolTask Executor自动装配不同的实例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring ThreadPoolTask Executor自动装配不同的实例相关的知识,希望对你有一定的参考价值。
我正在使用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;
}
}
当我使用@Autowire
将@Bean
@Qualifier
变成另一个类时,我看到默认的最大池大小的线程数而不是我提供的数字(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>
另一个有趣的观点是,当我从@Qualifier
和threadPoolTaskExecutor @Bean
中删除@Autowired TaskExecutor
注释时,我收到以下错误:
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
答案
两个选项:1。使用不同的限定符,然后使用camelcase。 myThreadPoolTaskExecutor
- 在ThreadPoolTaskExecutor上使用@primary。所以它将是默认捆绑的那个
以上是关于Spring ThreadPoolTask Executor自动装配不同的实例的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Spring Boot 中创建不同的 ThreadPoolTaskExecutor? [复制]
具有小队列容量的 ThreadPoolTaskExecutor 阻塞调用线程?
如何使用 ThreadPoolTaskExecutor 为任务设置超时
ThreadPoolTask Scheduler不适用于线程池