Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 351
shalakha Avatar asked Apr 09 '15 09:04

shalakha


People also ask

Is HttpClient multithreaded?

HttpClient is fully thread-safe when used with a thread-safe connection manager such as MultiThreadedHttpConnectionManager.

What is closeable HttpClient?

CloseableHttpClient is the base class of the httpclient library, the one all implementations use. Other subclasses are for the most part deprecated. The HttpClient is an interface for this class and other classes. You should then use the CloseableHttpClient in your code, and create it using the HttpClientBuilder .

Should HttpClient be closed?

You do not need to explicitly close the HttpClient, however, (you may be doing this already but worth noting) you should ensure that connections are released after method execution. Edit: The ClientConnectionManager within the HttpClient is going to be responsible for maintaining the state of connections.


1 Answers

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

like image 173
ok2c Avatar answered Sep 27 '22 18:09

ok2c