2012-08-14 3 views
0

HttpClient 3..0.1을 MultiThreadedHttpConnectionManager과 함께 사용하고 있으며 아래 코드를 사용하여 페이지를 가져오고 해당 페이지의 최종 리디렉션 된 URL을 가져옵니다.HttpMethod.releaseConnection() usage

여러 스레드가이 코드를 병렬로 액세스합니다. 이 코드를 얼마 동안 실행 한 후에는 ConnectionPoolTimeoutException이 계속해서 시작되고 더 이상 페이지가 페치되지 않습니다.

이 코드는 내 connectionManagerParam 값과 관련이 있습니까? 아니면 코드에서 잘못된 것이 있습니까?

GetMethod get = null; 
    try { 
     get = new GetMethod(); 
     get.setURI(uri); 
     get.setFollowRedirects(false); 
     int status = httpClient.executeMethod(null, get, new HttpState()); 

     String location = null; 
     int retry = 2; 
     if (get.getResponseHeader("location") != null) { 
      location = get.getResponseHeader("location").getValue(); 
     } 
     while (retry > 0 && ((int) (status/100) != 2) && ((int) (status/100) == 3) && location.length() > 0) { 
      // To get the final redirected url. 
      uri = URLUtil.createAbsoluteURIWithFix(location, null); 
      get = new GetMethod(); 
      get.setURI(uri); 
      get.setFollowRedirects(false); 
      status = httpClient.executeMethod(null, get, new HttpState()); 
      if (get.getResponseHeader("location") != null) { 
       location = get.getResponseHeader("location").getValue(); 
      } 
      retry--; 
     } 

     if (status == 200) { 
      uri = get.getURI(); 
      String html = URLUtil.getResponseBodyAsString(get, charsets); 
     } 
    } catch (Exception e) { 

    } finally { 
     if (get != null) { 
      get.releaseConnection(); 
     } 
    } 

org.apache.commons.httpclient.ConnectionPoolTimeoutException: Timeout waiting for connection 
    at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager.doGetConnection(MultiThreadedHttpConnectionManager.java:490) 
    at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager.getConnectionWithTimeout(MultiThreadedHttpConnectionManager.java:394) 
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:152) 
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396) 

내가 모든 후 get.releaseConnection()를 호출해야합니까 예외 스택 트레이스는 방법을 실행? 또는 현재 코딩 괜찮습니까?

답변

1

첫 번째 결과를 닫지 않고 동일한 var에 영향을주는 새 항목을 만드는 것 같지 않습니다. 그리고 루프 yoy에서 같은 일을하십시오.

이 코드에는 인스턴스를 하나만 유지하고 끝에 global try/finally를 사용하여 정리해야합니다.

+0

get.releaseConnection(); - 첫 번째 거래를 마감하지 않습니까? – razor

+0

get의 인스턴스가 루프 –

+0

아아 내의 영향에 의해 덮어 쓰여졌습니다 ... 루프 내에서 새로운 'GetMethod()'가 보이지 않았습니다. – razor