title: Get queue size of ThreadPoolTaskExecutor and add to queue in Spring boot
body: <p>I have the following class which has multiple custom <code>ThreadPoolTaskExecutors</code> I am showing it with one in this example.</p>
  
  <pre><code>@Configuration
  @EnableAsync
  public class ExecutorConfig {
      @Bean(name = "streetCheckerExecutor")
      public Executor getStreetAsyncExecutor() {
          ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
          executor.setCorePoolSize(50);
          executor.setQueueCapacity(1000000);
          executor.setThreadNamePrefix("streetCheckerExecutor-");
          executor.initialize();
          return executor;
      }
  }
  </code></pre>
  
  <p>I have the following class which gets content from the database, I want to be able to check the queue size of <code>streetCheckerExecutor</code> and if it's less than a certain number, to add the content to the queue</p>
  
  <pre><code>@Component
  public class StreetChecker {
  
      @Autowired
      StreetRepository streetRepository;
  
      @Autowired
      StreetCheckService streetChecker;
  
      @EventListener(ApplicationReadyEvent.class)
      public void checkStreets() {
          try {
              List&lt;Street&gt; streetList = streetRepository.getStreets();
  
              for (int i = 0; i &lt; streetList.size(); i++) {
                  streetChecker.run(streetList.get(i));
              }
  
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
              System.out.println("---------------------");
          }
      }
  
  }
  </code></pre>
  
  <p>And below is the worker class</p>
  
  <pre><code>@Component
  public class StreetCheckService {
      @Async("streetCheckerExecutor")
      public void run(Content content) {    
          try {
              //do work
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
  
          }
      }
  }
  </code></pre>
  
  <p>I am working with a lot of data and I don't want to grab everything from the database every time, but I want to check the queue size of <code>streetCheckerExecutor</code> and if it's less than a number, I want to get more content from the database and add it to the <code>streetCheckerExecutor</code> queque</p>
  
  <p>Below is how I'm thinking I can do it by converting the above <code>checkStreets</code> to the one below</p>
  
  <pre><code>@EventListener(ApplicationReadyEvent.class)
  public void checkStreets() {
      while (true) {
          try {
              // check the queue size of streetCheckerExecutor
              // if less than a number
              // add to the queue
              // else keep waiting and will try again in X minutes
          } catch (Exception e) {
  
          } finally {
              try {
                  Thread.sleep(1000 * 60);
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>But how would I be able to get the size of the queue in the <code>checkStreets()</code> method?</p>