How to limit max of 3 async threads per request - Spring @Async

Viewed 141

I have a Spring boot application and one of the RESTController needs to call 9 other external services. Instead of doing it serially, I want to do it parallelly. I have decided a max of 3 parallel threads. (T1- 3 calls + T2-3 calls + T3-3 calls ). Assume each call takes 100msec.

Response time Serial way - 9*100msec = 900msec.

My approach - 100 ( 3 parallel) + 100 ( 3 parallel) + 100 (3 parallel ) = 300 msec.

Request 1 --> Controller -- > calls multiple @Aysnc methods - 3 threads spawned

Request 2 --> Controller -- > calls multiple @Aysnc methods - 3 threads spawned

Request 3 --> Controller -- > calls multiple @Aysnc methods - 3 threads spawned

Will my code below spawns 3 threads per each request or the whole application has only 3 threads in pool for multiDataTaskExecutor and each request has to wait for these 3 threads.

Please see below, my code.

@Configuration
@EnableAsync
public class AsyncConfig {

  @Bean("multiDataTaskExecutor")
  public Executor taskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(3); //Not sure ?
    executor.setMaxPoolSize(3);  // Not sure ?
    executor.setQueueCapacity(50);  // Not sure ?
    executor.setThreadNamePrefix("MultiData-");
    executor.initialize();
    return executor;
  }

}

In controller I call

futures.add( service1.getData_1() );
futures.add( service2.getData_2() );
futures.add( service3.getData_3() );
:
:
futures.add( service9.getData_9() );

CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();

The service methods are

@Async("multiDataTaskExecutor")
public CompletableFuture<Data> getData_1() {

@Async("multiDataTaskExecutor")
public CompletableFuture<Data> getData_2() {

Also what happens if my async methods have just @Async instead of @Async("multiDataTaskExecutor"). Will it use default threadpool and spawns multiple (9) threads per each request and will response times be only 100 msec ( all 9 parallel)

I don't want to get threads exhausted error or use up all threads as I get 300-400 requests per minute.

0 Answers
Related