How does HttpAsyncClient 4 work?
K

1

7

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!

Katelyn answered 29/12, 2015 at 5:56 Comment(3)
Your question is kind of complicated, i.e., too abstract and maybe belongs in a different forum. I didn't look up any specs, but I assume that HttpAsyncClient creates a Thread because the Future class is part of the concurrency package. Therefore your program is creating ten background threads which run independently on the various cores of your computer. My very basic laptop has four cores, so I get four times the improvement in performance when I run multiple threads. If you use the regular HttpClient, then you do 10 sequential requests, waiting for each to finish before the next begins.Quintinquintina
Future can be used in HttpAsyncClient because of asynchronous work with HTTP (javadoc at execute() method confirms it). So no, this isn't just about threads (though I agree - surely there are some threads (daemons) created inside HttpAsyncClient).Katelyn
HttpAsyncClient employs a small number of i/o dispatch threads (equal to the number of CPU cores by default) to do all message handling and processing.Copybook
C
7

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
Copybook answered 4/1, 2016 at 13:52 Comment(4)
It's pretty clear now, thank you. Maybe you'd be able to answer my another question about HttpAsyncClient? stackoverflow.com/questions/34576416 Is there any difference in HTTP requests created by default or pipelining HttpAsyncClient? It's really important for me, I'll even start a bounty tomorrow.Katelyn
Do not use message pipelining unless you have a very specific need for it, like a mobile connection with a massive latencyCopybook
I have a good reason to use it - I don't really care about responses, and I need to increase network performance (I mean I need to send requests faster).Katelyn
unless you have a high latency connection pipelining is unlikely to increase performance but is very likely to cause all sorts of interoperability issuesCopybook

© 2022 - 2024 — McMap. All rights reserved.