title: TaskRejectedException in ThreadPoolTaskExecutor
body: <p>I am trying to call an API asynchronously using Spring's Async and using the ThreadPoolTaskExecutor in my Thread Config which goes: </p>
  
  <pre><code>@Configuration
  @EnableAsync
  public class ThreadConfig extends AsyncConfigurerSupport {
  
  @Value("${core.pool.size}")
  private int corePoolSize;
  
  @Value("${max.pool.size}")
  private int maxPoolSize;
  
  @Value("${queue.capacity}")
  private int queueCapacity;
  
  @Override
  @Bean
  public Executor getAsyncExecutor() {
  
      ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
      executor.setCorePoolSize(corePoolSize);
      executor.setMaxPoolSize(maxPoolSize);
      executor.setQueueCapacity(queueCapacity);
      executor.setThreadNamePrefix("default_task_executor_thread");
      executor.initialize();
  
      return executor;
  
  }
  </code></pre>
  
  <p>The settings here are:</p>
  
  <pre><code>corePoolSize = 5;
  maxPoolSize = 10;
  QueueCapacity = 10;
  </code></pre>
  
  <p>I'm calling the Async service as follows:</p>
  
  <pre><code>for (String path : testList) {
      Future&lt;Boolean&gt; pro = services.invokeAPI(path);
  }
  </code></pre>
  
  <p>The testList has about 50 records.</p>
  
  <p>When I run this, the compiler processes 10 threads and calls the invokeAPI method 10 times after which it gives: </p>
  
  <pre><code>org.springframework.core.task.TaskRejectedException: Executor[java.util.concurrent.ThreadPoolExecutor@3234ad78[Running, pool size = 10, active threads = 10, queued tasks = 10, completed tasks = 0]] did not accept task: org.springframework.aop.interceptor.AsyncExecutionInterceptor$1@5c17b70
  </code></pre>
  
  <p>I was assuming that it will wait for the current tasks to complete and re-assign the threads instead of throwing me the exception and terminating the program. </p>
  
  <p>What should I do to have all my 50 records call the invokeAPI method?</p>
  
  <p>Edit: the number of records in testList can change.</p>
  
  <p>Any suggestions please?</p>