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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With