I want to use Java 11 HttpClient and send multiple requests on the same address with the use of the Keep-Alive parameter of HTTP 1.1. I know that there is similar functionality for Apache HTTP Client, but is there anything like that for Java API's client?
Is there any handling of connection pooling on java.net.HttpClient?
Asked Answered
Yes - HTTP/1.1 connections are kept alive by default, unless the maximum size of the pool is exceeded, in which case the oldest idle connection in the pool (the connection which is closest to its expiration date) is closed to make room for the new connection. However multiple connections to the same host will be opened if the requests are made concurrently.
Thanks for the answer. It would improve its quality and help others if you could include links to a(n official) documentation about these features. It might also help to discover if/how it is configurable, and learn about other opitmization concepts. –
Donelu
How to configure pool limit? Have not found answer in docs. –
Subchaser
Hi Ferdynand: this is an implementation detail - so there is no "official" documentation. As a matter of fact there is actualy no such thing as "the connection pool". The HttpClient supports both HTTP/1.1 and HTTP/2 and so it has two pools - which are handled differently because of the specifics of each protocol. My answer above concerns only the HTTP/1.1 connection pool. By default it is unbounded, and connections in that pool are kept alive for 1200 seconds, unless closed by the server side. –
Recto
what about this: github.com/openjdk/jdk/blob/… ? all the properties seem to be documented here: docs.oracle.com/en/java/javase/17/core/… –
Furriery
This answer to basically the same question covers it well: https://mcmap.net/q/857927/-how-to-keep-connection-alive-in-java-11-http-client –
Furriery
© 2022 - 2024 — McMap. All rights reserved.