Search code examples
apachetomcat6apache-httpclient-4.x

httpclient unable to send more than two requests


I am writing a Client-Server Application. I am using CloseableHttpClient on the client side for sending GET requests. I want to send 5 GET requests to the server. However, only I am getting response for only first two requests. Here is my client code :

 import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;


public class Test {

    public static void main(String[] args) {
        HttpGet getreq = new HttpGet("http://shalakha:8089/Gateway/ReverseInvokeListener");
        CloseableHttpClient c1 = HttpClients.createMinimal();
        try {
            for(int i=0;i<5;i++){
            CloseableHttpResponse resp = c1.execute(getreq);
            System.out.println(resp);
        }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Here is the response I am getting :

HTTP/1.1 200 OK [Server: Apache-Coyote/1.1, Set-Cookie: JSESSIONID=0D4ABDC362FEC3030A5EE3709F3096FD; Path=/Gateway, CLIENTREQ: NO, Content-Length: 0, Date: Thu, 09 Apr 2015 09:22:22 GMT]
HTTP/1.1 200 OK [Server: Apache-Coyote/1.1, CLIENTREQ: NO, Content-Length: 0, Date: Thu, 09 Apr 2015 09:22:26 GMT]

where 'CLIENTREQ' is a CUSTOM header that I have added from the server side while sending the response.

Any pointers as to what must be causing this? The connectionTimeout in server.xml is set to '-1' which indicates infinite timeout.


Solution

  • Your code has leaked all connections available to it (which are two by default) and has exhausted the connection pool. You need to close response objects to ensure that connections get released back to the pool.

    http://hc.apache.org/httpcomponents-client-4.4.x/tutorial/html/fundamentals.html#d5e145