简体   繁体   English

Apache HTTP 客户端 - Apache Http 客户端中 0 个租用连接的问题

[英]Apache HTTP Client - Issues with 0 leased connections in Apache Http client

I am trying to use PoolingHttpClientConnectionManager in our module.我正在尝试在我们的模块中使用 PoolingHttpClientConnectionManager。

Below is my code snippet下面是我的代码片段

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();
            }
        }
    }
}

the output i received was as below:我收到的输出如下:

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

I am unable to find my leased connections are always 0 though there are requests executing.尽管有请求正在执行,但我无法找到我的租用连接始终为 0。 Also, the routes are always shown as 0 though I have registered the route.此外,尽管我已经注册了路线,但路线始终显示为 0。 It seems to me that there is some problem but I could not identify it.在我看来,有一些问题,但我无法确定。 Available connections are also shown as 0 during execution(though it is not printed here).执行期间可用连接也显示为 0(尽管此处未打印)。 Please help me in finding out what went wrong.请帮我找出问题所在。 Thanks!谢谢!

The first print of "Available connections" message is of course 0, as only after you'll get the response and it'll be decided, based on default strategies DefaultClientConnectionReuseStrategy & DefaultConnectionKeepAliveStrategy , if the connection should be reusable, and for how long, only then the connection will be moved to available connections list . “可用连接”消息的第一次打印当然是 0,因为只有在您得到响应之后才会根据默认策略DefaultClientConnectionReuseStrategyDefaultConnectionKeepAliveStrategy决定连接是否应可重用,以及使用多长时间,只有这样连接才会移动到可用连接列表

I'm guessing that the number of routes is also decided after at least one connection was created.我猜路由数量也是在至少创建一个连接之后决定的。

In your log you can see that the main thread printed all the "Leased connections before assigning" messages before the child threads ran, and so you see 0 leased connections.在您的日志中,您可以看到主线程在子线程运行之前打印了所有“分配租用连接”消息,因此您会看到 0 个租用连接。 A connection exists in leased connections list only from the time of creation until the releasing time, that usually happens on response.readEntity() , response.close() , the closing of the connection manager, and maybe on EntityUtils.consume() as well.一个连接只存在于租用连接列表中,从创建时间到释放时间,这通常发生在response.readEntity()response.close() ,连接管理器的关闭,也可能在EntityUtils.consume() 上作为好。

So maybe try to move the " Leased connections before assigning " from the main thread to child thread.因此,也许尝试将“分配之前租用的连接”从主线程移动到子线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM