How to use multiple threadPoolExecutor for Async Spring

前端 未结 1 1787
攒了一身酷
攒了一身酷 2021-01-05 00:47

I am using Spring @Async on two classes. Both are ultimately implementing an interface. I am creating two separate ThreadPoolTaskExecutor so each class has its own ThreadPoo

相关标签:
1条回答
  • 2021-01-05 01:10

    By default when specifying @Async on a method, the executor that will be used is the one supplied to the 'annotation-driven' element as described here.

    However, the value attribute of the @Async annotation can be used when needing to indicate that an executor other than the default should be used when executing a given method.

    @Async("otherExecutor")
    void doSomething(String s) {
        // this will be executed asynchronously by "otherExecutor"
    }
    

    In this case, "otherExecutor" may be the name of any Executor bean in the Spring container, or may be the name of a qualifier associated with any Executor, e.g. as specified with the element or Spring’s @Qualifier annotation

    https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

    And probably you need to specify the otherExecutor bean in you app with the pool settings you wish.

    @Bean
    public TaskExecutor otherExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(25);
    
        return executor;
    }
    
    0 讨论(0)
提交回复
热议问题