title: ExecutorService performing REST requests
body: <p>I´m trying to send an undefined number of HTTP Rest requests using concurrecy. This is the basic code skeleton:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
  connManager.setMaxTotal(maxConn);
  connManager.setDefaultMaxPerRoute(maxConn);
      CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(connManager).build();
  
  ExecutorService es = Executors.newFixedThreadPool(maxConn);
  
  for (...){
     es.execute(new RequestThread(httpclient, ...other data for creating a request...));
  }
  
  es.shutdown();
  es.awaitTermination(timeout, TimeUnit.SECONDS);
  //Tested timeout = 30
  
  httpclient.close();</code></pre>
  </div>
  </div>
  </p>
  
  <p>And the basic code of RequestThread is:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>    public class RequestThread implements Runnable {
  
  public RequestThread(CloseableHttpClient httpclient, ..other neccesay data..){
     ...
  }
  
      @Override
      public void run() {
   try {
       processRequest(httpclient, ...);
                  // In this method, each CloseableHttpResponse is consumed and closed properly.
   } catch (Exception e) {
       e.printStackTrace();
   }
      }</code></pre>
  </div>
  </div>
  </p>
  
  <p>And it throws this exception multiple times:</p>
  
  <pre><code>java.lang.IllegalStateException: Connection pool shut down
      at org.apache.http.util.Asserts.check(Asserts.java:34)
      at org.apache.http.pool.AbstractConnPool.lease(AbstractConnPool.java:184)
      at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.requestConnection(PoolingHttpClientConnectionManager.java:251)
      at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:175)
      at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
      at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
      at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
      at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
      at com.ericsson.http.DMHttpCommunicationManager.executeRequestMethod(DMHttpCommunicationManager.java:109)
      at com.ericsson.http.RequestThread.processRequest(RequestThread.java:39)
      at com.ericsson.http.RequestThread.run(RequestThread.java:25)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>It seems the main thread is being closed too soon and causes the created runnables to throw these exceptions.</p>
  
  <p>Is there any way to force ExecutorService to wait for the runnable to finish (before closing the HttpClient)? Should I use other strategy?</p>
  
  <p>Thanks in advance. </p>
  
  <p>Regards.</p>