Search code examples
javahttpclientjava-11keep-alivejava-http-client

Is there a way to make keep-alive setting specific to one instance of java.net.http.HttpClient


I know you can set keep-alive by using System property jdk.httpclient.keepalive.timeout, and this value is only read once when the class jdk.internal.net.http.ConnectionPool is loaded. Afterwards, it cannot be changed anymore. However, this would make all instances of java.net.http.HttpClient have the same keep-alive setting. Is there a way to do this, that is specific to a single instance of the client?

One idea I had is to set headers on the request explicitly. Example:

HTTP/1.1 200 OK
Connection: Keep-Alive
Keep-Alive: timeout=5, max=1000

However, not sure if this will work or if there is a better way to override settings like this. I was surprised to see configurations like this not available on the client builder.


Solution

  • Unfortunately the only way to do this today is with a system property on the java command line. The Keep-Alive header field returned by the server is currently ignored, though there is an RFE to revisit this: https://bugs.openjdk.org/browse/JDK-8326160. Also note that Keep-Alive is only valid for HTTP/1.1 (not for HTTP/2)