title: Apache HTTP Client - Issues with 0 leased connections in Apache Http client
body: <p>I am trying to use PoolingHttpClientConnectionManager in our module. </p>
  
  <p>Below is my code snippet</p>
  
  <pre><code>import java.io.IOException;
  import org.apache.hc.client5.http.ClientProtocolException;
  import org.apache.hc.client5.http.HttpRoute;
  import org.apache.hc.client5.http.classic.HttpClient;
  import org.apache.hc.client5.http.classic.methods.HttpGet;
  import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
  import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
  import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
  import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
  import org.apache.hc.core5.http.HttpEntity;
  import org.apache.hc.core5.http.HttpHost;
  import org.apache.hc.core5.http.io.entity.EntityUtils;
  
  public class Testme {
      static PoolingHttpClientConnectionManager connectionManager;
      static CloseableHttpClient httpClient;
  
      public static void main(String args[]) {
          connectionManager = new PoolingHttpClientConnectionManager();
          connectionManager.setDefaultMaxPerRoute(3);
          connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost("http://127.0.0.1",8887)), 5);
          httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
          System.out.println("Available connections "+connectionManager.getTotalStats().getAvailable());
          System.out.println("Max Connections "+connectionManager.getTotalStats().getMax());
          System.out.println("Number of routes "+connectionManager.getRoutes().size());
          Testme testme = new Testme();
          Testme.ThreadMe threads[] = new Testme.ThreadMe[5];
          for(int i=0;i&lt;5;i++) 
              threads[i] = testme.new ThreadMe();
  
          for(Testme.ThreadMe thread:threads) { 
              System.out.println("Leased connections before assigning "+connectionManager.getTotalStats().getLeased());
              thread.start(); 
          }
      }
  
      class ThreadMe extends Thread{
  
          @Override
          public void run() {
              try {
                  CloseableHttpResponse response= httpClient.execute(new HttpGet("http://127.0.0.1:8887"));
                  System.out.println("Req for "+Thread.currentThread().getName() + " executed with "+response);
                  try {
                      HttpEntity entity = response.getEntity();
                      EntityUtils.consume(entity);
                  }catch(IOException e) {
                      e.printStackTrace();    
                  }
                  finally {   
                      response.close();
                  } 
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>the output i received was as below:</p>
  
  <pre><code>Available connections 0
  Max Connections 25
  Number of routes 0
  Leased connections before assigning 0
  Leased connections before assigning 0
  Leased connections before assigning 0
  Leased connections before assigning 0
  Leased connections before assigning 0
  Req for Thread-2 executed with 200 OK HTTP/1.1
  Req for Thread-4 executed with 200 OK HTTP/1.1
  Req for Thread-3 executed with 200 OK HTTP/1.1
  Req for Thread-0 executed with 200 OK HTTP/1.1
  Req for Thread-1 executed with 200 OK HTTP/1.1
  </code></pre>
  
  <p>I am unable to find my leased connections are always 0 though there are requests executing. Also, the routes are always shown as 0 though I have registered the route. It seems to me that there is some problem but I could not identify it. Available connections are also shown as 0 during execution(though it is not printed here). Please help me in finding out what went wrong. Thanks!</p>