Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 605
mat_boy Avatar asked Apr 14 '13 16:04

mat_boy


1 Answers

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.

like image 131
Deepak Bala Avatar answered Oct 07 '22 15:10

Deepak Bala