HttpAsyncClient 4 如何工作? [英] How does HttpAsyncClient 4 work?

本文介绍了HttpAsyncClient 4 如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在之前版本的 HttpClient 中,目标主机被设置为客户端本身.在上一个版本中(对于 HttpAsyncClient,它是 4.1.1)主机设置为 HttpRequest(HttpGetHttpPost 等.) 每次我提出请求时.

In previous versions of HttpClient target host was set up into client itself. In last version (for HttpAsyncClient it's 4.1.1) host is set up into HttpRequest (HttpGet, HttpPost etc.) every time I do a request.

我想使用持久连接,所以我使用HttpAsyncClient.我像这样创建和使用它:

I want to use persistent connection, so I use HttpAsyncClient. I create and use it like this:

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
}

正如我测试的那样,它的运行速度比平时要快 HttpClient(如果我执行所有请求,然后等待所有响应).

As I tested, it works faster than usual HttpClient (if I do all the requests, and then wait for all the responses).

但我无法完全理解它在内部是如何工作的.与 https://google.com/ 建立了多少连接?如果我将 client 用于一台主机,然后用于另一台主机,会发生什么情况?(正如我测试的那样,响应可以按任何顺序出现,所以我想至少有 2 个并行连接).HttpAsyncClients.createDefault()HttpAsyncClients.createPipelining() 有什么区别?

But I can't fully understand, how it works inside. How many connections with https://google.com/ are established? What happens if I use client for one host, and then for another? (as I tested, responses can come in any order, so I suppose there are at least 2 connections in parallel). What's the difference between HttpAsyncClients.createDefault() and HttpAsyncClients.createPipelining() ?

谢谢!

推荐答案

默认情况下,根据 RFC 2616 规范,HttpAsyncClient 仅允许两个并发连接到同一主机.这个限制与 i/o 反应器内部使用的 i/o 调度线程的数量无关.

By default HttpAsyncClient permits only two concurrent connections to the same host per RFC 2616 specification. This limit has nothing to do with the number of i/o dispatch threads used internally by the i/o reactor.

上面的代码最多会创建两个传出连接.

The code above will create two outgoing connections at most.

HTTP 消息流水线本身与连接持久性无关,尽管流水线请求执行意味着使用持久连接.

HTTP message pipelining has nothing do with connection persistence per se, though pipelined request execution implies the use of persistent connections.

HTTP 流水线是关于消息排序的.流水线模式下的 HttpAsyncClient 可以发送多个请求,无需等待每个响应.

HTTP pipelining is about message sequencing. HttpAsyncClient in the pipelining mode can send multiple requests without waiting for each response.

默认模式:

C -> request1 -> S
C <- response1 <- S
C -> request2 -> S
C <- response2 <- S

流水线模式:

C -> request1 -> S
C -> request2 -> S
C <- response1 <- S
C <- response2 <- S

这篇关于HttpAsyncClient 4 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆