I am writing a multi-threaded REST client that uses different APIs for different purposes, For making these requests I am using an HttpClient with its different methods (GET,PUT,POST)
Thread 1:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httpclient.execute(httppost);
methodThatNeedsHttpClient(httpclient);
public void methodThatNeedsHttpClient(HttpClient client) {
//perform other GET/POST/PUT requests
}
Thread 2:
DefaultHttpClient httpclient2 = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httpclient2.execute(httppost);
// Other methods
I read that for managing httpConnections I should using a Connection Manager. I am on version 4.5 of the client, which connection manager should I be using? How does the connection manager ensure connections dont leak and are used efficiently?
I tried the following implementation :
PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
connectionManager.setMaxTotal(5);
// Perform REST operations
client.getConnectionManager().shutdown();
But I am not sure how connections are managed in the pool , for a multithreaded system, does the connection manager be initialized in every thread?
In most cases, connection pool should be a shared one, accessible by all httpClient instances.
When creating httpClient,
And this will release the connection back to the pool,
While will close the connection,
Since we have
setConnectionManagerShared(true),httpClient.close()wouldn't shunt down the connection pool.