【问题标题】:Spring ThreadPoolTaskExecutor Autowiring Different InstanceSpring ThreadPoolTask​​Executor 自动装配不同的实例
【发布时间】: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


【解决方案1】:

两个选项: 1. 使用与驼峰式不同的限定词。 myThreadPoolTask​​Executor

  1. 在您的 ThreadPoolTask​​Executor 上使用 @primary。所以它将是默认捆绑的那个

【讨论】:

    猜你喜欢
    • 2021-01-02
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 2018-02-20
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    相关资源
    最近更新 更多