Search code examples
javascalaconnection-poolingapache-httpclient-4.xapache-commons-httpclient

Why is my HTTPClient running out of connections when using pooled connection manager?


I am using Apache HttpComponents 4.5.1. in a Scala project, however Scala is unlikely to be my issue. Here is a JUnit 4 style test.

@Test def closableHttpClientWithPoolingCmTest{
  val poolingHttpConnectionManager: PoolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager()
  poolingHttpConnectionManager setMaxTotal (12)
  poolingHttpConnectionManager setDefaultMaxPerRoute (8)
  def client = HttpClients custom () setConnectionManager  (poolingHttpConnectionManager) build ()
  for (i  <- 0 to 12 ){
    def response = client.execute(new HttpGet("http://www.yahoo.com"));
    EntityUtils.consume(response.getEntity)
    response.close();
  }
}

This test hangs, because we run out of connections. What am I missing?I consume the entity and close the response. Do I need to close anything else besides response? Please help.


Solution

  • Your client definition should be a val not a def

    defis evaluated every time client is called, making a new pool for every invocation. Instead with val your pool will be constructed once and reused.

    def client = HttpClients custom () setConnectionManager (poolingHttpConnectionManager) build ()

    should become

    val client = HttpClients custom () setConnectionManager (poolingHttpConnectionManager) build ()