Java Apache HTTP客户端-Apache HTTP客户端中存在0个租用连接的问题
我正在尝试在我们的模块中使用PoollightTPClientConnectionManager 下面是我的代码片段Java Apache HTTP客户端-Apache HTTP客户端中存在0个租用连接的问题,java,connection-pooling,apache-httpclient-5.x,Java,Connection Pooling,Apache Httpclient 5.x,我正在尝试在我们的模块中使用PoollightTPClientConnectionManager 下面是我的代码片段 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; impor
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();
}
}
}
}
虽然有请求正在执行,但我找不到租用的连接始终为0。此外,尽管我已注册了路由,但路由始终显示为0。在我看来,似乎有一些问题,但我无法确定它。在执行期间,可用连接也显示为0(尽管此处未打印)。请帮我找出哪里出了问题。谢谢 当然,“Available connections”(可用连接)消息的第一次打印是0,因为只有在您得到响应并根据默认策略DefaultClientConnectionReuseStrategy和DefaultConnectionKeepAliveStrategy确定连接是否应可重用以及可重用多长时间之后,只有这样,连接才会移动到“可用连接”列表
我猜路由的数量也是在至少创建一个连接之后决定的
在日志中,您可以看到主线程在子线程运行之前打印了所有“分配前租用连接”消息,因此您可以看到0个租用连接。
连接仅在创建时到释放时存在于租用连接列表中,这通常发生在response.readEntity()、response.close()、连接管理器关闭时,也可能发生在EntityUtils.consume()上
因此,可以尝试将“分配前租用的连接”从主线程移动到子线程
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