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.

4
  • 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. Commented Aug 24, 2023 at 15:56
  • Note also that we changed the default value in recent version of the JDK. See docs.oracle.com/en/java/javase/20/docs/api/java.net.http/… Commented Aug 24, 2023 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 Commented Aug 24, 2023 at 18:11
  • HttpClient.newHttpClient() is the equivalent to HttpClient.newBuilder().build(); Use the jdk.httpclient.keepalive.timeout system 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. Commented Sep 11, 2023 at 13:40

2 Answers 2

1

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().

Sign up to request clarification or add additional context in comments.

Comments

0

You Can set your Keep-Alive time by using

-Djdk.httpclient.keepalive.timeout=120

Or making a Static Block and set System Property

static{
       System.setProperty("jdk.httpclient.keepalive.timeout","120");
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.