mat_boy
mat_boy

Reputation: 13688

org.apache.http.client.HttpClient - one per request?

I use org.apache.http.client.HttpClient. I have a question that concerns the use of this class.

Should I create a new HttpClient per request (e.g. because it is a light object) or it is better to use a unique instance per many http requests (e.g., because create/delete is expensive)? If only one instance of HttpClient must be used, is HttpClient thread safe (e.g., it can handle many http requests at the same time) or it is preferable in this case to create a pool of HttpClients?

Actually, I create a new HttpClient per request. I suspect that I must create a unique instance to be closed at the end of the use of my application by using getConnectionManager().shutdown(), but I don't know if I can maintain Thread-based parallelism.

Upvotes: 9

Views: 7926

Answers (1)

Deepak Bala
Deepak Bala

Reputation: 11185

The DefaultHttpClient is marked with the @ThreadSafe annotation so yes you can use it in a Thread safe manner. The performance documentation from HTTPClient also actively recommends using a single instance where possible.

Upvotes: 6

Related Questions