Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between CloseableHttpClient and HttpClient in Apache HttpClient API?

I'm studying an application developed by our company. It uses the Apache HttpClient library. In the source code it uses the HttpClient class to create instances to connect to a server.

I want to learn about Apache HttpClient and I've gone trough this set of examples. All the examples use CloseableHttpClient instead of HttpClient. So I think CloseableHttpClient is an extended version of HttpClient. If this is the case I have two questions:

  • What is the difference between these two?
  • Which class is recommended to use for my new development?
like image 428
Nayana Adassuriya Avatar asked Feb 05 '14 10:02

Nayana Adassuriya


People also ask

How do I use CloseableHttpClient?

Create instance of CloseableHttpClient using helper class HttpClients . Create HttpGet or HttpPost instance based on the HTTP request type. Use addHeader method to add required headers such as User-Agent, Accept-Encoding etc. For POST, create list of NameValuePair and add all the form parameters.

Should we close CloseableHttpClient?

Resource Management The reason why we need to close CloseableHttpClient instances once they go out of scope is to shut down the associated connection manager. In addition, we should also use CloseableHttpResponse in order to ensure proper deallocation of system resources.

What is Apache HttpClient?

The Apache HttpClient library allows to handling HTTP requests. To use this library add a dependency to your Maven or Gradle build file. You find the latest version here: https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient. You retrieve and send data via the HttpClient class.

Does RestTemplate use HttpClient?

RestTemplate delegates to a ClientHttpRequestFactory, and one of the implementations of this interface uses Apache's HttpClient.


2 Answers

  • The main entry point of the HttpClient API is the HttpClient interface.
  • The most essential function of HttpClient is to execute HTTP methods.
  • Execution of an HTTP method involves one or several HTTP request / HTTP response exchanges, usually handled internally by HttpClient.

  • CloseableHttpClient is an abstract class which is the base implementation of HttpClient that also implements java.io.Closeable.
  • Here is an example of request execution process in its simplest form:

    CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet("http://localhost/"); CloseableHttpResponse response = httpclient.execute(httpget); try {     //do something } finally {     response.close(); }

  • HttpClient resource deallocation: When an instance CloseableHttpClient is no longer needed and is about to go out of scope the connection manager associated with it must be shut down by calling the CloseableHttpClient#close() method.

    CloseableHttpClient httpclient = HttpClients.createDefault(); try {     //do something } finally {     httpclient.close(); }

see the Reference to learn fundamentals.


@Scadge Since Java 7, Use of try-with-resources statement ensures that each resource is closed at the end of the statement. It can be used both for the client and for each response

try(CloseableHttpClient httpclient = HttpClients.createDefault()){      // e.g. do this many times     try (CloseableHttpResponse response = httpclient.execute(httpget)) {     //do something     }      //do something else with httpclient here } 
like image 153
Sagar Pudi Avatar answered Sep 23 '22 07:09

Sagar Pudi


Had the same question. The other answers don't seem to address why close() is really necessary? Also, Op seemed to be struggling to figure out the preferred way to work with HttpClient, et al.


According to Apache:

// 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. 

In addition, the relationships go as follows:

HttpClient (interface)

implemented by:

CloseableHttpClient - ThreadSafe.

DefaultHttpClient - ThreadSafe BUT deprecated, use HttpClientBuilder instead.

HttpClientBuilder - NOT ThreadSafe, BUT creates ThreadSafe CloseableHttpClient.

  • Use to create CUSTOM CloseableHttpClient.

HttpClients - NOT ThreadSafe, BUT creates ThreadSafe CloseableHttpClient.

  • Use to create DEFAULT or MINIMAL CloseableHttpClient.

The preferred way according to Apache:

CloseableHttpClient httpclient = HttpClients.createDefault(); 

The example they give does httpclient.close() in the finally clause, and also makes use of ResponseHandler as well.


As an alternative, the way mkyong does it is a bit interesting, as well:

HttpClient client = HttpClientBuilder.create().build(); 

He doesn't show a client.close() call but I would think it is necessary, since client is still an instance of CloseableHttpClient.

like image 20
euphoria99 Avatar answered Sep 24 '22 07:09

euphoria99