coo*_*guy 6 java apache-httpcomponents apache-commons-httpclient apache-httpclient-4.x apache-httpasyncclient
在以前的版本中,HttpClient目标主机已设置为客户端本身。在最新版本(对于HttpAsyncClient4.1.1)中,每次执行请求时,主机都设置为HttpRequest(HttpGet,HttpPost等等。)。
我想使用持久连接,所以我使用HttpAsyncClient。我这样创建和使用它:
CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
client.start();
List<Future<HttpResponse>> responses = new ArrayList<>();
for (int i = 0; i < 10; i++)
{
HttpGet get = new HttpGet("https://google.com/");
responses.add(client.execute(get, null));
}
for (Future<HttpResponse> response : responses) {
response.get(); //wait for the response
}
Run Code Online (Sandbox Code Playgroud)
如我所测试,它的工作速度比平常更快HttpClient(如果我执行所有请求,然后等待所有响应)。
但是我无法完全了解它是如何工作的。https://google.com/建立了多少个连接?如果我使用client一台主机,然后再使用另一台主机会怎样?(正如我测试的那样,响应可以以任何顺序进行,因此我想至少有2个并行连接)。HttpAsyncClients.createDefault()和之间有什么区别HttpAsyncClients.createPipelining()?
谢谢!
默认情况下,根据RFC 2616规范,HttpAsyncClient仅允许两个并发连接到同一主机。此限制与I / O反应器内部使用的I / O调度线程的数量无关。
上面的代码最多将创建两个传出连接。
HTTP消息管道化本身与连接持久性无关,尽管管道化请求执行暗含了持久性连接的使用。
HTTP流水线是关于消息排序的。管道模式下的HttpAsyncClient可以发送多个请求,而无需等待每个响应。
默认模式:
C -> request1 -> S
C <- response1 <- S
C -> request2 -> S
C <- response2 <- S
Run Code Online (Sandbox Code Playgroud)
流水线模式:
C -> request1 -> S
C -> request2 -> S
C <- response1 <- S
C <- response2 <- S
Run Code Online (Sandbox Code Playgroud)