本文介绍了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如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 09:27