HttpClient can't get response from server

Viewed 4117

This problem has blocked our whole team half a day!

We use apache httpclient 4.3.x to post and get data from an storage server which provides http api. In order to improve performance, we used PoolingHttpClientConnectionManager:

public HttpClient createHttpClient() {
    Registry registry = RegistryBuilder.create()....build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
    connectionManager.setMaxTotal(50);
    connectionManager.setDefaultMaxPerRoute(50);

    CloseableHttpClient httpClient = HttpClients.custom()
       .setConnectionManager(connectionManager)
       .build();
    return httpClient;
}

Then we hold an instance of the httpClient in our program, reuse it with every http request:

Global httpClient:

HttpClient httpClient = createHttpClient();

Post some data:

HttpPost httpPut = new HttpPost("...");
HttpResponse response = httpClient.execute(httpPut);

// Notice we get the response content here!
String content = EntityUtils.toString(response.getEntity());
System.out.println(content);

httpPut.releaseConnection();
response.close();

Then get:

HttpGet httpGet = new HttpGet("...");

// Blocked at this line !!!!
HttpResponse response = httpClient.execute(httpGet);

String content = EntityUtils.toString(response.getEntity());
System.out.println(content);

httpPut.releaseConnection();
response.close();    

Please notice the line: // Blocked at this line !!!!

The program has blocked at that line and never go to next line. In debugging mode, I can see it has been blocked at:

SocketInputStream.socketRead0()

I've searched for a lot of questions and documents, but no lucky.


My colleage just fix it by setting NoConnectionReuseStrategy.INSTANCE:

 HttpClients.custom()
       .setConnectionManager(connectionManager)
       // Following line fixed the problem, but why?
       .setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE)
       .build();

Now it doens't blocked, but why?

What does "reuse connection" mean? And is there performance issue by using NoConnectionReuseStrategy?

Thank you, guys~

2 Answers
Related