私はApache PoolingHttpClientConnectionManager
を使用して、接続をプールしてClosableHttpClient
とCloseableHttpResponse
と一緒にサービスコールを行います。重大な反応を読むために、私はEntityUtils.consume(httpResponse.getEntity())
を使用しています。 httpResponse
がnull
になると、接続を解除する方法を知りたいです。私はreleaseConnection()
(方法PoolingHttpClientConnectionManager
)を試しましたが、動作していません。以下のコードを見つけることができます。Apache PoolingHttpClientConnection Manager応答エンティティがnullになった場合の接続の解放
String decision = "";
CloseableHttpResponse httpResponse = null;
try {
HttpPost requestPost = new HttpPost(endpoint1); requestPost.setHeader("Connection", "close");
requestPost.setHeader("Content-type",
"application/x-www-form-urlencoded");
requestPost.addHeader("Accept-Encoding", "gzip,deflate");
String xmlRequestStr = "Request=" + URLEncoder.encode(urlString);
String soRequest = xmlRequestStr;
requestPost.setEntity(new StringEntity(soRequest));
httpResponse = client.execute(requestPost);
decision = EntityUtils.toString(httpResponse.getEntity(),encod);
//connEvictor.shutdown();
} catch (Exception e) {
System.out.println("Pooling Connection manager Exception : " + e.getMessage());
e.printStackTrace();
//connEvictor.shutdown();
throw new ToolkitException("Connection Manager Exception", e.getMessage());
} finally {
EntityUtils.consume(httpResponse.getEntity());
httpResponse.close();
}
を呼び出します。要求がタイムアウトになり、httpResponse = client.execute(requestPost)で失敗した場合、CloseableHttpClientはnullを返します。ステップと例外ブロックに行くと、私はそれがエラーが発生した場合、例外ブロック経由で最終ブロックに来ると思います。 EntityUtils.consume(httpResponse.getEntity());を保持している場合tryブロックでは、例外が発生したときに接続を解放できません。右 ?私が間違っている場合は私を修正してください。 –
例外がスローされたために応答がnullの場合、それを閉じるか消費する必要はありません – pedrofb
私はそれを残していれば、TCP/IP状態をCLOSE_WAITにします。 –