0

I've configuration that is implementing AsyncConfigurer as bellow:

@Configuration
@EnableAsync
public class AsyncConfiguration implements AsyncConfigurer {
    
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(5);
        executor.setCorePoolSize(5);
        executor.setQueueCapacity(500);
        executor.initialize();
        return executor;
    }

That new thread pool is working fine with that:

@Component
public class MessageJmsListener {

    @Async
    @EventListener
    public void onApplicationEvent(CustomMessage messageJmsWrapper) {
        log.info("Current thread: {}", Thread.currentThread().getName());
    }

    @Async
    @EventListener
    public void onApplicationEventSecond(SecondCustomMessage messageJmsWrapper) {
        log.info("Second Listener thread: {} , Thread.currentThread().getName());
    }
}

And I would like to achieve such an effect that for the first listener I give a separate bean (number of threads) and for the second. Is such a thing possible?

Thanks in advance! Best Regards

rkosegi
  • 14,165
  • 5
  • 50
  • 83
kamil wilk
  • 117
  • 7

1 Answers1

0

It looks like that using qualifiers could help you. Create two different executors and add different qualifiers for each. Use the first one for the first listener with one one parameter set and the second one for the other one with different parameter set. See example in this thread.

Another approach in your case could be that you call the right executor by adding executor name in annotation where you are invoking it. Example:

@Configuration
@EnableAsync
class ApplicationAsyncConfiguration {

    @Bean(name = "maxThreadingExecutor")
    public Executor maxThreadingExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(5);
        executor.setCorePoolSize(5);
        executor.setQueueCapacity(500);
        executor.initialize();
        return executor;
    }

    @Bean(name = "defaultExecutor")
    public Executor defaultExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(2);
        executor.setCorePoolSize(2);
        executor.setQueueCapacity(500);
        executor.initialize();
        return executor;
    }

}
@Component
public class MessageJmsListener {

    @Async("maxThreadingExecutor")
    @EventListener
    public void onApplicationEvent(CustomMessage messageJmsWrapper) {
        log.info("Current thread: {}", Thread.currentThread().getName());
    }

    @Async("defaultExecutor")
    @EventListener
    public void onApplicationEventSecond(SecondCustomMessage messageJmsWrapper) {
        log.info("Second Listener thread: {}", Thread.currentThread().getName());
    }

}

See more in this answer, might be helpful.

Alexander Gusev
  • 295
  • 1
  • 9