1

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,

  1. There seems to be no provision to set connectTimeout when creating object using HttpClient.newHttpClient()

  2. Also, as per this question, it appears a default connection pool (UNLIMITED connections) is created with keepalive.timeout=1200 seconds only when the HTTPCLient object is created using HttpClient.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.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • There is no correlation between the connect timeout and the keep alive timeout. The keep alive timeout only affects idle connections that sit in the pool, and can only be configured system-wide by supplying system properties on the command line (or editing the conf/net.properties file). If you supply a different keepalive timeout via a system property on the java command line it will work for all instances of HttpClient. – daniel Aug 24 '23 at 15:56
  • Note also that we changed the default value in recent version of the JDK. See https://docs.oracle.com/en/java/javase/20/docs/api/java.net.http/module-summary.html – daniel Aug 24 '23 at 15:58
  • Hi @daniel, with `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 via `HttpClient.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 mins – Kaustubh Hande Aug 24 '23 at 18:11

1 Answers1

0

HttpClient.newHttpClient() is equivalent to HttpClient.newBuilder().build(). This means that it creates an HttpClient with default settings.

Therefore, if you need non default settings, you should use

HttpClient client = HttpClient.newBuilder()
                              .version(HttpClient.Version.HTTP_1_1). 
                              .connectTimeout(Duration.ofMillis(2000)) 
                              .build();

As for the keep alive property, as mentioned in the referenced question, you can change the default to 2 minutes with

-Djdk.httpclient.keepalive.timeout=120

This will affect all HttpClient instances, regardless of whether you create them with HttpClient.newBuilder() or with HttpClient.newHttpClient().

Eran
  • 387,369
  • 54
  • 702
  • 768