2016-12-27 20 views
0

私は私が使用していますこれらの要求を行うために、異なる目的のために異なるAPIを使用してマルチスレッドのRESTクライアントを書いていて、異なる方法(POST、GET、PUT)効率的な方法

とのHttpClientをスレッド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 
} 

スレッド2:

DefaultHttpClient httpclient2 = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url); 
httpclient2.execute(httppost); 
// Other methods 

私はhttpConnectionsを管理するために、私はConnection Managerを使用しなければならないことをお読みください。私は接続マネージャーが使用するはずのクライアントのバージョン4.5を使用していますか?接続マネージャは、接続が確実にリークしないようにして、効率的に使用しますか?

私は、次の実装しようとした:

PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(); 
     connectionManager.setMaxTotal(5); 
// Perform REST operations 

client.getConnectionManager().shutdown(); 

をしかし、私は接続がプール内で管理されているかわからない、マルチスレッドシステムのため、接続マネージャは、すべてのスレッドで初期化されるのでしょうか?

答えて

0

ほとんどの場合、接続プールはすべてのhttpClientインスタンスがアクセスできる共有プールである必要があります。

のHttpClientを作成し、

CloseableHttpClient httpClient = HttpClients.custom() 
       .setConnectionManager(connectionManager) 
       .setConnectionManagerShared(true) 
       .build(); 

そして、これは我々がsetConnectionManagerShared(true)を持っているのでながら、

httpResponse.close(); 
httpClient.close(); 

を、接続を閉じます

EntityUtils.consume(httpResponse.getEntity()); 

、バックプールへの接続を解放します、httpClient.close()は接続プールをシャットダウンしません。