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()is equivalent toHttpClient.newBuilder().build(). This means that it creates anHttpClientwith default settings.Therefore, if you need non default settings, you should use
As for the keep alive property, as mentioned in the referenced question, you can change the default to 2 minutes with
This will affect all
HttpClientinstances, regardless of whether you create them withHttpClient.newBuilder()or withHttpClient.newHttpClient().