如何使用AsyncConfigurer定义多个合格的线程池 [英] How to define multiple qualified thread pools with AsyncConfigurer

查看:332
本文介绍了如何使用AsyncConfigurer定义多个合格的线程池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring框架具有接口AsyncConfigurer(和AsyncConfigurerSupport),用于配置拥有线程池执行程序并能够异步运行方法(使用@Async)所需的一切.

Spring framework has an interface AsyncConfigurer (and AsyncConfigurerSupport) for configuring everything necessary for having a thread pool executor and being able to run methods asynchronously (with @Async).

但是通常,在不同功能之间共享相同的线程池不是一个好习惯,因此通常我用一个名称来限定它们的名称,并在Async注释中指定该名称以使用一个特定的线程池.

But usually, it's not a good practice to share the same thread pool among different functionalities, so usually I qualified them with a name, and I specify that name in Async annotation for it to use one specific thread pool.

问题是,我想通过此方便的接口AsyncConfigurer对其进行配置,而不是失去资格,但我做不到.

Thing is, I would like to configure them through this convenient interface AsyncConfigurer and not loosing qualification, but I'm not able to.

尝试一下:

    @Configuration
    @EnableAsync
    public class PusherAsyncConfigurerSupport extends AsyncConfigurerSupport {
        @Autowired
        ExecutorConfig config;
        @Override
        @Qualifier(value = "executorOne")
        public Executor getAsyncExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(config.getCorePoolSize());
            ...
            executor.initialize();
            return executor;
        }

        @Override
        public AsyncUncaughtExceptionHandler     getAsyncUncaughtExceptionHandler() {
            return new MyAsyncUncaughtExceptionHandler();
        }
    }

当放入异步注释时,仍无法识别该线程池.

That thread pool is still not recognized when put in an Async annotation.

那么,以这种方式配置几个线程池需要什么?还是不是这样呢?

So, what is it needed for configuring several thread pools this way? Or isn't it the way for that at all?

推荐答案

带有@Bean批注的标记

mark with @Bean annotation

@Bean
@Qualifier(value = "executorOne")
public Executor getAsyncExecutor() {
//..
  return executor;
}

这篇关于如何使用AsyncConfigurer定义多个合格的线程池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆