Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Apache CloseableHttpResponse not consume the entity on close?

Looking at the quick start guide it gives the following code example:

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://targethost/homepage");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
// The underlying HTTP connection is still held by the response object
// to allow the response content to be streamed directly from the network socket.
// In order to ensure correct deallocation of system resources
// the user MUST call CloseableHttpResponse#close() from a finally clause.
// Please note that if response content is not fully consumed the underlying
// connection cannot be safely re-used and will be shut down and discarded
// by the connection manager. 
try {
    System.out.println(response1.getStatusLine());
    HttpEntity entity1 = response1.getEntity();
    // do something useful with the response body
    // and ensure it is fully consumed
    EntityUtils.consume(entity1);
} finally {
    response1.close();
}

The two comments in the code above say that we must close the response object for

"correct deallocation of system resources"

and

"if response content is not fully consumed the underlying connection cannot be safely re-used and will be shut down and discarded by the connection manager".

Now Apache have very kindly implementend a CloseableHttpResponse for us, which means we can use a try-with-resources block. But the close method only closes the response object, why doesn't it also consume the entity?

like image 509
james Avatar asked Jun 10 '17 05:06

james


People also ask

Does CloseableHttpClient need to be closed?

The reason is that the default client implementation returns an instance of CloseableHttpClient, which requires closing. Therefore, in our custom code, we should use the CloseableHttpClient class, not the HttpClient interface.

Is it necessary to close HttpClient?

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.

How do I close CloseableHttpClient?

Start a try-finally block, write the remaining code in the programs in the try block and close the CloseableHttpClient object in the finally block.

What is the close method in closeablehttpclient?

Now, about the close method in CloseableHttpClient. CloseableHttpClient is an abstract class that implements Closeable interface. That is, although it doesn't have a close method itself the classes that extend it are required to implement the close method. One class is InternalHttpClient. You can check the source code for the details.

What are the best examples of closeablehttpresponse in Java?

Java CloseableHttpResponse.getEntity - 30 examples found. These are the top rated real world Java examples of org.apache.http.client.methods.CloseableHttpResponse.getEntity extracted from open source projects. You can rate examples to help us improve the quality of examples.

How to close HTTP connections manually using httpclients?

While closing HTTP connections manually follow the steps given below − The createDefault () method of the HttpClients class returns an object of the class CloseableHttpClient, which is the base implementation of the HttpClient interface. Using this method, create an HttpClient object as shown below −

How do I Close a httpclient in a try block?

CloseableHttpClient httpClient = HttpClients.createDefault (); Start a try-finally block, write the remaining code in the programs in the try block and close the CloseableHttpClient object in the finally block.


Video Answer


1 Answers

Because it is hard to say at that point whether or not the caller intends to re-use the underlying connection. In some cases one may want to read just a small chunk from a large response body and immediately terminate the connection.

In other words, the same thing happens over and over again: there is no one way that can make everyone happy.

The code snippet will do ensure proper de-allocation of resources while trying to keep the underlying connection alive.

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://targethost/homepage");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
    System.out.println(response1.getStatusLine());
} finally {
    EntityUtils.consume(response1.getEntity());
} 
like image 88
ok2c Avatar answered Oct 26 '22 10:10

ok2c