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?
Here is an example of request execution process in its simplest form:
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.
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.
HttpClient
is not a class, it is an interface. You cannot use it for development in the way you mean.What you want is a class that implements the
HttpClient
interface, and that isCloseableHttpClient
.In the next major version of the library
HttpClient
interface is going to extendCloseable
. Until then it is recommended to useCloseableHttpClient
if compatibility with earlier 4.x versions (4.0, 4.1 and 4.2) is not required.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:
In addition, the relationships go as follows:
The preferred way according to Apache:
The example they give does
httpclient.close()
in thefinally
clause, and also makes use ofResponseHandler
as well.As an alternative, the way mkyong does it is a bit interesting, as well:
He doesn't show a
client.close()
call but I would think it is necessary, sinceclient
is still an instance ofCloseableHttpClient
.The other answers don't seem to address why
close()
is really necessary? * 2Doubt on the answer "HttpClient resource deallocation".
It is mentioned in old 3.x httpcomponents doc, which is long back and has a lot difference from 4.x HC. Besides the explanation is so brief that doesn't say what this underlying resource is.
I did some research on 4.5.2 release source code, found the implementations of
CloseableHttpClient:close()
basically only closes its connection manager.(FYI) That's why when you use a shared
PoolingClientConnectionManager
and call clientclose()
, exceptionjava.lang.IllegalStateException: Connection pool shut down
will occur. To avoid,setConnectionManagerShared
works.I prefer not do
CloseableHttpClient:close()
after every single requestI used to create a new http client instance when doing request and finally close it. In this case, it'd better not to call
close()
. Since, if connection manager doesn't have "shared" flag, it'll be shutdown, which is too expensive for a single request.In fact, I also found in library clj-http, a Clojure wrapper over Apache HC 4.5, doesn't call
close()
at all. See funcrequest
in file core.cljCloseableHttpClient
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 theHttpClientBuilder
. If you need to wrap the client to add specific behaviour you should use request and response interceptors instead of wrapping with theHttpClient
.This answer was given in the context of httpclient-4.3.