2

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).

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.

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

Trying this:

    @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?

Pedro R.
  • 142
  • 1
  • 11

1 Answers1

2

mark with @Bean annotation

@Bean
@Qualifier(value = "executorOne")
public Executor getAsyncExecutor() {
//..
  return executor;
}
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • 1
    So easy, it worked! (but with `@Bean(name = "executorOne")`. I was expecting more from spring magic. – Pedro R. Nov 09 '16 at 13:45