2016-12-25 12 views
-1

私はApache PoolingHttpClientConnectionManagerを使用して、接続をプールしてClosableHttpClientCloseableHttpResponseと一緒にサービスコールを行います。重大な反応を読むために、私はEntityUtils.consume(httpResponse.getEntity())を使用しています。 httpResponsenullになると、接続を解除する方法を知りたいです。私は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(); 
    } 

答えて

0

CloseableHttpClientはヌル応答を返してはなりません。エラーが発生すると例外が発生するので、安全にclose()を呼び出してください。エラーがclose方法を発生した場合

を実行されませんのでdocumentation

finally句でEntityUtils.consume(httpResponse.getEntity());を呼び出さないでくださいを参照してください。あなたがされている有効期限の切れたとアイドル状態の接続を閉じるには、モニターを実装することができhttp://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.htmlに接続立ち退きポリシーをお読みくださいサーバー上でブロックまたはクローズされています。モニタはcloseExpiredConnections()closeIdleConnections(period

+0

を呼び出します。要求がタイムアウトになり、httpResponse = client.execute(requestPost)で失敗した場合、CloseableHttpClientはnullを返します。ステップと例外ブロックに行くと、私はそれがエラーが発生した場合、例外ブロック経由で最終ブロックに来ると思います。 EntityUtils.consume(httpResponse.getEntity());を保持している場合tryブロックでは、例外が発生したときに接続を解放できません。右 ?私が間違っている場合は私を修正してください。 –

+0

例外がスローされたために応答がnullの場合、それを閉じるか消費する必要はありません – pedrofb

+0

私はそれを残していれば、TCP/IP状態をCLOSE_WAITにします。 –

関連する問題