I'm using Java 11 HttpClient to send request to a remote server.
I wanted to know if there is a difference on the HttpClient object when creating object in below 2 different ways
HttpClient client = HttpClient.newHttpClient();
vs
HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).
.connectTimeout(Duration.ofMillis(2000))
.build();
For Starters,
There seems to be no provision to set
connectTimeoutwhen creating object usingHttpClient.newHttpClient()Also, as per this question, it appears a default connection pool (UNLIMITED connections) is created with
keepalive.timeout=1200seconds only when theHTTPCLientobject is created usingHttpClient.newHttpClient().
Does this not happen when it is created using the following?
HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).
.connectTimeout(Duration.ofMillis(connectionTimeout))
.build()
I want to leverage automatic connection pool creation of Java 11 HTTPClient with my custom keepalive.timeout=120 sec and still be able to also set connectTimeout value.
Please advise, TIA.
HttpClient.newHttpClient()I do not have an option to set the 'connecttimeout` on the HttpClient object returned. Add, if a default connectionpool still created behind the scene if I create my client object viaHttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1). .connectTimeout(Duration.ofMillis(connectionTimeout)) .build()?. I do see that latest Java version has defaulted keepalive.timeout to 30s but I'm using Java 11 which defaults to 20 min which is too high and I want to set it to 2 minsHttpClient.newHttpClient()is the equivalent toHttpClient.newBuilder().build(); Use thejdk.httpclient.keepalive.timeoutsystem property on the java command to control the keep-alive timeout in the HTTP/1.1 connection pool (note that this is a system-wide setting). In Java 11 the keep alive property applies only to the HTTP/1.1 connection pool.