Set HTTP Keep Alive Timeout in Java HTTP Client

Introduction

This is a sample to set HTTP Keep Alive Timeout in HTTP Client introduced from Open JDK 11.

It is a good rule to make the Keep Alive Timeout value on the client side shorter than that on the server side in order to prevent communication errors when the connection is closed from the server side and the request from the client is processed at the same time.

Therefore, it is important to know the setting method on the client side.

JDK Version to use

Use the latest Open JDK 13 at the time of writing. It also uses jshell to execute the code. As an aside, jshell is really useful.

~ $ java --version
openjdk 13.0.1 2019-10-15
OpenJDK Runtime Environment (build 13.0.1+9)
OpenJDK 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing)

Setting method

The HTTP Client Keep Alive Timeout can be set with the java property jdk.httpclient.keepalive.timeout.

-Djdk.httpclient.keepalive.timeout=60 Or System.setProperty("jdk.httpclient.keepalive.timeout", "99999"); It can be set by such a method.

This setting is read by the jdk.internal.net.http.ConnectionPool class. Since the variable that holds the timeout value is static, it must be set before loading the above class, and it seems that it cannot be changed after loading (unverified). As far as the source code is concerned, the default value in Open JDK 13 is 1200 seconds.

The source of ConnectionPool (JDK 13) is [here](https://github.com/openjdk/jdk13/blob/master/src/java.net.http/share/classes/jdk/internal/net/http/ConnectionPool You can refer to it from .java).

sample

This is a sample in jshell. HTTP GET to qiita.com. Set Keep Alive Timeout to 10 seconds.

jshell execution sample


jshell> import java.net.http.*

jshell> System.setProperty("jdk.httpclient.keepalive.timeout", "10")
$1 ==> null

jshell> HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build()
client ==> jdk.internal.net.http.HttpClientImpl@57baeedf(1)

jshell> HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://qiita.com")).build()
request ==> https://qiita.com GET

jshell> HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString())
response ==> (GET https://qiita.com) 200

Check the validity of the settings

Since it is not possible to check whether the Timeout value has changed with the above sample alone, try monitoring the TCP status with the netstat command of the OS. In the example below, the netstat command is executed every second and the command result is narrowed down by the Public IP address of qiita.com.

netstat


while true; do date; netstat -n -p TCP | egrep "13.114.xxx.xxx|176.34.xxx.xxx|13.113.xxx.xxx"; sleep 1; done

Thursday, November 21, 2019 18:31:34 JST
Thursday, November 21, 2019 18:31:35 JST
tcp4       0      0  xxx.xxx.xxx.xxx.62156    13.114.xxx.xxx.443     ESTABLISHED
Thursday, November 21, 2019 18:31:36 JST
tcp4       0      0  xxx.xxx.xxx.xxx.62156    13.114.xxx.xxx.443     ESTABLISHED
Thursday, November 21, 2019 18:31:37 JST
tcp4       0      0  xxx.xxx.xxx.xxx.62156    13.114.xxx.xxx.443     ESTABLISHED
Thursday, November 21, 2019 18:31:38 JST
tcp4       0      0  xxx.xxx.xxx.xxx.62156    13.114.xxx.xxx.443     ESTABLISHED
Thursday, November 21, 2019 18:31:39 JST
tcp4       0      0  xxx.xxx.xxx.xxx.62156    13.114.xxx.xxx.443     ESTABLISHED
Thursday, November 21, 2019 18:31:40 JST
tcp4       0      0  xxx.xxx.xxx.xxx.62156    13.114.xxx.xxx.443     ESTABLISHED
Thursday, November 21, 2019 18:31:41 JST
tcp4       0      0  xxx.xxx.xxx.xxx.62156    13.114.xxx.xxx.443     ESTABLISHED
Thursday, November 21, 2019 18:31:42 JST
tcp4       0      0  xxx.xxx.xxx.xxx.62156    13.114.xxx.xxx.443     ESTABLISHED
Thursday, November 21, 2019 18:31:43 JST
tcp4       0      0  xxx.xxx.xxx.xxx.62156    13.114.xxx.xxx.443     ESTABLISHED
Thursday, November 21, 2019 18:31:44 JST
tcp4       0      0  xxx.xxx.xxx.xxx.62156    13.114.xxx.xxx.443     ESTABLISHED
Thursday, November 21, 2019 18:31:45 JST
tcp4       0      0  xxx.xxx.xxx.xxx.62156    13.114.xxx.xxx.443     ESTABLISHED
Thursday, November 21, 2019 18:31:46 JST
tcp4       0      0  xxx.xxx.xxx.xxx.62156    13.114.xxx.xxx.443     TIME_WAIT

After the TCP connection is established, it becomes ʻESTABLISHEDfor 10 seconds, and since it is actively closed 1 second later, it can be seen that it has moved toTIME_WAIT`.

I was able to confirm that the settings are valid.

Recommended Posts

Set HTTP Keep Alive Timeout in Java HTTP Client
Java HTTP Client API timeout setting
Which Java HTTP client is better?
Java (set)
[Java] Set AdoptOpen JDK in STS (Eclipse)
[Java Bronze] 5 problems to keep in mind
Set up a Java GUI in a separate thread to keep the main
Let's keep this in mind What's new in Java 9
Set pop-up display for Java language in vim.
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Java mqtt client
Pi in Java
FizzBuzz in Java
Get the URL of the HTTP redirect destination in Java