Differencebetween Closeablehttpclient and Httpclient in Apache Httpclient API

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

  • 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
}

Right way to close CloseableHttpResponse/CloseableHttpClient

It has been explained in detail in the docs here.

Quoting the pseudo code from the docs here's a typical way to allocate/deallocate an instance of CloseableHttpClient:

try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
<...>
}

The same applies to CloseableHttpResponse :

try (CloseableHttpResponse response = httpclient.execute(httpget)) {
<...>
}

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.

Before Java7, explicit close would be required:

CloseableHttpClient httpclient = HttpClients.createDefault();
try {
<...>
} finally {
httpclient.close();
}

CloseableHttpResponse response = httpclient.execute(httpget);
try {
<...>
} finally {
response.close();
}

When should the HttpClient be closed?

The idea of closing your HttpClient is about releasing the allocated ressources. Therefore, It depends on how often you plan on firing those HTTP requests.

Keep in mind that firing a request every 10 seconds is considered an eternity ;)

Risks of using Apache CloseableHttpClient in a Singleton

  1. There are none (based on my 15+ years of experience with HttpClient).

  2. This really depends on various factors (overhead of TLS session handshake, and so on). I imagine one would really want to ensure that series of related requests get executed over a persistent connection. During an extended period of inactivity one may want to evict all idle connections from the connection pool. This has an added benefit of reducing the chance of running into a stale (half-closed) connection problem.

Can CloseableHttpClient in httpclient-4.5.2 be shared between multiple requests?

I can be and it should be. One should have only once instance of HttpClient per aplication or a distinct HTTP service



Related Topics



Leave a reply



Submit