2017-07-20 1 views
0

私はApache PoolingHttpClientConnectionManagerを使用して会議のプールを作成しています。しかし、私は、既存のアイドル接続が使用されておらず、新しい接続を作成することをログで確認しています。応答がPoolStatsを受信した後、以下のコードPoolingHttpClientConnectionManagerが既存のアイドル接続を再使用しない

PoolingHttpClientConnectionManager connManager = connManager = new PoolingHttpClientConnectionManager(); 
connManager.setMaxTotal(4); 
    connManager.setDefaultMaxPerRoute(4); 

//Creating CloseableHttpClient with a default keep alive of 2 minutes 
    CloseableHttpClient client = HttpClients.custom() 
      .setConnectionManager(connManager) 
      .setKeepAliveStrategy(new KeepAliveStrategy(keepAlive)) 
      .build(); 


//Sending 1st request 
String xml="<xml data>"; 
HttpPost post = new HttpPost("<URL>"); 
    HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8")); 
    post.setEntity(entity); 
    HttpResponse response =client.execute(post); 
    String result = EntityUtils.toString(response.getEntity()); 
    EntityUtils.consume(response.getEntity()); 

がある利用可能な接続の総数は1であることを述べています。

今、私は再び5秒後に同じ要求を火災やレスポンスにが利用可能な接続の総数はPoolStats

のために2

以下のコードであることを述べてPoolStatsを取得したとき

PoolStats stats = connManager.getTotalStats(); System.out.println("Total Connections Available : "+stats.getAvailable()); 

ここで私の質問は最初のリクエストレスポンスの後にすでにConnectionがプールにあったので、なぜそれが作成されたのですか?もう1つの接続。なぜそれは既存の議会を使用しなかったのですか?

答えて

0

問題は、ここでSSLを使用していたためです。デフォルトでは、SSLコンテキストは同じ接続を共有できません。それが、それぞれの要求に対して別の接続を作成していた理由です。ソリューションは、カスタムのUserTokenHandlerを作成し、それをConnection Managerに割り当てます。

UserTokenHandler userTokenHandler = new UserTokenHandler() { 

     @Override 
     public Object getUserToken(final HttpContext context) { 
      return context.getAttribute("my-token"); 
     } 

    }; 

client = HttpClients.custom() 
      .setConnectionManager(connManager) 
      .setUserTokenHandler(userTokenHandler) 
      .setKeepAliveStrategy(new KeepAliveStrategy(keepAlive)) 
      .build(); 
関連する問題