Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep connection alive in Java 11 http client

I'm using the Java 11 HttpClient with HTTP/2 and need to keep connection alive for few minutes, but the builder does not have the option to set it. Is there a way to specify this and make client keep the connection alive for some time?

like image 260
qiGuar Avatar asked Dec 04 '18 16:12

qiGuar


People also ask

Is Java 11 HttpClient thread-safe?

Once created, an HttpClient instance is immutable, thus automatically thread-safe, and you can send multiple requests with it. By default, the client tries to open an HTTP/2 connection. If the server answers with HTTP/1.1, the client automatically falls back to this version.

What is keep alive strategy HttpClient?

The Keep-Alive duration determines whether or not the connection is idle, in fact - if the Keep-alive strategy says to keep connections alive for 10 seconds, and we receive responses from the server every 2 seconds, the connection will be kept alive for 10 seconds after the last successful response.

What is the default timeout for HTTP request in Java?

The Request Timeout property specifies the number of seconds the server waits between accepting a connection to a client and receiving information from it. The default setting is 30 seconds.

How do I monitor HTTP connection pool?

Go to Monitoring and Tuning > Performance Viewer > Current activity , select server, then in PMI viewer select Settings > Log to define logging period and format. And in Modules > Thread pools > WebContainer you can view current counter values. This is rather for short term monitoring, than for constant logging.

What is the use of keep alive http?

What is HTTP Keep-Alive HTTP keep-alive, a.k.a., HTTP persistent connection, is an instruction that allows a single TCP connection to remain open for multiple HTTP requests/responses. By default, HTTP connections close after each request.

How to change the keep-alive timeout for HTTP connections in Java?

This pool keeps the connections alive by default for 1200 seconds (20 minutes). If you want to change the keep-alive timeout you can do so using the property jdk.httpclient.keepalive.timeout.

How long does a httpclient connection stay alive?

If you build a standard HttpClient e.g. using HttpClient.newHttpClient (); by default a connection pool is created. This pool keeps the connections alive by default for 1200 seconds (20 minutes).

How do I enable keep alive in htaccess?

Enabling the Keep-Alive Header In the event that keep-alive is not enabled on your server, it can be turned on by adding the following code to your.htaccess file: <IfModule mod_headers.c> Header set Connection keep-alive> <IfModule> Within the ‘Connection keep-alive’ header, the following two directives can affect its functionality.


2 Answers

If you build a standard HttpClient e.g. using HttpClient.newHttpClient(); by default a connection pool is created. This pool keeps the connections alive by default for 1200 seconds (20 minutes).

If you want to change the keep-alive timeout you can do so using the property jdk.httpclient.keepalive.timeout. However the value is only read once when the class jdk.internal.net.http.ConnectionPool is loaded. Afterwards it can't be changed anymore.

Therefore you have to set this property for the whole JVM:

-Djdk.httpclient.keepalive.timeout=99999

Or at runtime before the ConnectionPool class has been loaded:

System.setProperty("jdk.httpclient.keepalive.timeout", "99999");

A third option is to using a file named ${java.home}/conf/net.properties and set the property in there.

like image 71
Robert Avatar answered Oct 17 '22 20:10

Robert


Both HTTP/2 and HTTP/1.1 connections are kept alive by default. There are some exceptions when several concurrent connections are opened to the same host - then only one of them will be kept alive.

like image 26
daniel Avatar answered Oct 17 '22 22:10

daniel