Spring ThreadPoolTask Executor自动装配不同的实例

问题描述 投票:0回答:1

我正在使用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>

另一个有趣的观点是,当我从@QualifierthreadPoolTaskExecutor @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
spring dependency-injection threadpoolexecutor
1个回答
0
投票

两个选项:1。使用不同的限定符,然后使用camelcase。 myThreadPoolTask​​Executor

  1. 在ThreadPoolTask​​Executor上使用@primary。所以它将是默认捆绑的那个
© www.soinside.com 2019 - 2024. All rights reserved.