How does HttpAsyncClient 4 work?

我的未来我决定 提交于 2020-08-24 06:57:59

问题


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.

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
}

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

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() ?

Thanks!


回答1:


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 message pipelining has nothing do with connection persistence per se, though pipelined request execution implies the use of persistent connections.

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

Default mode:

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

Pipelining mode:

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


来源:https://stackoverflow.com/questions/34505271/how-does-httpasyncclient-4-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!