Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple threadPoolExecutor for Async Spring

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 ThreadPool to work off of. However due to I think something with proxy and how Spring implements Async classes, I have to put the @Async annotation on the base interface. Because of this, both classes end up using the same ThreadPoolTaskExecutor. Is it possible to tell Spring that for this Bean (in this case I am calling the classes that implement that interface a Service), use this ThreadPoolTaskExecutor.

like image 699
Kirit Avatar asked Aug 19 '17 21:08

Kirit


People also ask

Does Spring Boot asynchronous execution support async task executor?

In this post we will be discussing about spring boot asynchronous execution support using async task executor feature to execute task in a different thread. We will take a look into configuring SimpleAsyncTaskExecutor, ConcurrentTaskExecutor, ThreadPoolExecutor in a spring project.

What is the use of threadpooltaskexecutor in spring?

In Spring’s default , threads will be rejected with ThreadPoolExecutor.AbortPolicy and you lose new threads. For scalable application i’m suggested to use ThreadPoolExecutor.CallerRunsPolicy. This policy provides to us scalable queue when maximum pool is filled. ThreadPoolTaskExecutor has following policies definitions;

What is threadpoolexecutor abortpolicy in Spring Boot?

In Spring’s default , threads will be rejected with ThreadPoolExecutor.AbortPolicy and you lose new threads. For scalable application i’m suggested to use ThreadPoolExecutor.CallerRunsPolicy. This policy provides to us scalable queue when maximum pool is filled.

How to run methods annotated with @async in spring?

By default spring uses SimpleAsyncTaskExecutor to run methods annotated with @Async. We can also define our custom executor bean as follow and use it at method level. SimpleAsyncTaskExecutor does make sense in cases, if you want to execute some long-time-executing tasks, e.g. if you want to compress log files at the end of a day.


Video Answer


1 Answers

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;
}
like image 80
Imran Avatar answered Oct 16 '22 00:10

Imran