및 CloseableHttpResponse
과 함께 연결을 풀링하기 위해 Apache PoolingHttpClientConnectionManager
을 사용하여 서비스 호출을합니다. 서버 응답을 읽으려면 EntityUtils.consume(httpResponse.getEntity())
을 사용하고 있습니다. httpResponse
이 null
이 될 때 연결을 해제하는 방법을 알고 싶습니다. 내가 releaseConnection()
(방법 PoolingHttpClientConnectionManager
) 시도했지만 작동하지 않습니다. 아래 코드를 찾을 수 있습니다.아파치 풀링 응답 개체가 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 블록에서 예외가 발생하면 연결을 해제 할 수 없습니다. 권리 ? 내가 틀렸다면 나를 바로 잡아주세요. –
예외가 throw 되었기 때문에 응답이 null 인 경우 닫거나 소비하지 않아도됩니다. – pedrofb
나가는 중이면 TCP/IP 상태를 CLOSE_WAIT (으)로 이동합니다. –