2016-12-25 6 views
-1

CloseableHttpResponse과 함께 연결을 풀링하기 위해 Apache PoolingHttpClientConnectionManager을 사용하여 서비스 호출을합니다. 서버 응답을 읽으려면 EntityUtils.consume(httpResponse.getEntity())을 사용하고 있습니다. httpResponsenull이 될 때 연결을 해제하는 방법을 알고 싶습니다. 내가 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(); 
    } 

답변

0

CloseableHttpClient은 null 응답을 반환하지 않아야합니다. 오류시 예외가 발생하므로 안전하게 close()을 호출해야합니다. 오류가 close 방법은 당신이왔다 만료 및 유휴 연결을 닫습니다 모니터를 구현할 수 http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html에 연결 퇴거 정책을 읽어 보시기 바랍니다

을 실행되지 않습니다 발생하면 때문에 documentation

finally 절에 EntityUtils.consume(httpResponse.getEntity());를 호출하지 마십시오 참조 차단되거나 서버에서 닫혔습니다. 모니터가 closeExpiredConnections()closeIdleConnections(period

+0

을 호출합니다. 요청이 시간 초과되고 httpResponse = client.execute (requestPost)에서 실패 할 때 CloseableHttpClient가 null을 반환합니다. 단계 및 예외 블록 이동하고 오류가 발생하면 예외 블록을 통해 최종 블록 올 것 같아요. EntityUtils.consume (httpResponse.getEntity()); try 블록에서 예외가 발생하면 연결을 해제 할 수 없습니다. 권리 ? 내가 틀렸다면 나를 바로 잡아주세요. –

+0

예외가 throw 되었기 때문에 응답이 null 인 경우 닫거나 소비하지 않아도됩니다. – pedrofb

+0

나가는 중이면 TCP/IP 상태를 CLOSE_WAIT (으)로 이동합니다. –