0

モジュールで PoolingHttpClientConnectionManager を使用しようとしています。

以下は私のコードスニペットです

import java.io.IOException;
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.HttpRoute;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.io.entity.EntityUtils;

public class Testme {
    static PoolingHttpClientConnectionManager connectionManager;
    static CloseableHttpClient httpClient;

    public static void main(String args[]) {
        connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setDefaultMaxPerRoute(3);
        connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost("http://127.0.0.1",8887)), 5);
        httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
        System.out.println("Available connections "+connectionManager.getTotalStats().getAvailable());
        System.out.println("Max Connections "+connectionManager.getTotalStats().getMax());
        System.out.println("Number of routes "+connectionManager.getRoutes().size());
        Testme testme = new Testme();
        Testme.ThreadMe threads[] = new Testme.ThreadMe[5];
        for(int i=0;i<5;i++) 
            threads[i] = testme.new ThreadMe();

        for(Testme.ThreadMe thread:threads) { 
            System.out.println("Leased connections before assigning "+connectionManager.getTotalStats().getLeased());
            thread.start(); 
        }
    }

    class ThreadMe extends Thread{

        @Override
        public void run() {
            try {
                CloseableHttpResponse response= httpClient.execute(new HttpGet("http://127.0.0.1:8887"));
                System.out.println("Req for "+Thread.currentThread().getName() + " executed with "+response);
                try {
                    HttpEntity entity = response.getEntity();
                    EntityUtils.consume(entity);
                }catch(IOException e) {
                    e.printStackTrace();    
                }
                finally {   
                    response.close();
                } 
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

私が受け取った出力は以下の通りでした:

Available connections 0
Max Connections 25
Number of routes 0
Leased connections before assigning 0
Leased connections before assigning 0
Leased connections before assigning 0
Leased connections before assigning 0
Leased connections before assigning 0
Req for Thread-2 executed with 200 OK HTTP/1.1
Req for Thread-4 executed with 200 OK HTTP/1.1
Req for Thread-3 executed with 200 OK HTTP/1.1
Req for Thread-0 executed with 200 OK HTTP/1.1
Req for Thread-1 executed with 200 OK HTTP/1.1

実行中のリクエストがありますが、リースされた接続が常に 0 であることがわかりません。また、ルートを登録したのにルートが常に0と表示されます。何か問題があるようですが、特定できませんでした。利用可能な接続も、実行中に 0 として表示されます (ただし、ここには表示されません)。何が悪かったのかを見つけるのを手伝ってください。ありがとう!

4

1 に答える 1