How to set SocketTimeout

157 Views Asked by At

In

CloseableHttpAsyncClient

the Socket Timeout (http.socket.timeout) – the time waiting for data – after establishing the connection; maximum time of inactivity between two data packets

So how to set the timeout from request to response

I can not resolve the problem...

1

There are 1 best solutions below

0
rick On

There are three kinds of timeout which you should know the difference.

connectTimeout: max time to establish a connection with remote host/server.

connectionRequestTimeout: time to wait for getting a connection from the connection manager/pool. (HttpClient maintains a connection pool to manage the connections. Similar to database connection pool)

socketTimeout: max time gap between two consecutive data packets while transferring data from server to client.

Your question should be totalTimeout. totalTimeout = connectionRequestTimeout + connectTimeout + socketTimeout

The correct approach is to set three different timeout values for each stage. The code might look like this:

RequestConfig config = RequestConfig.custom()
  .setConnectTimeout(timeoutx)
  .setConnectionRequestTimeout(timeouty)
  .setSocketTimeout(timeoutz).build();
CloseableHttpClient client = 
  HttpClientBuilder.create().setDefaultRequestConfig(config).build();