HTTP Request is a common integration approach used for building any reasonably sized application. However, just like in real life, a request is just a request and there is no guarantee a request would receive an appropriate response.

So what should be a developer do in this case?

In real-life we may be tempted to wait for a long time for a response. However, this is usually not the case in a typical software application. A user waiting for a response for an abnormally long time would be far more devastating to the business prospects of the application as compared to a failed response. Therefore, it is imperative that we make appropriate use of Timeouts when designing HTTP calls in our applications.

In this post, we will look at how to use configure Java HttpClient timeout properties.

1 – Types of Timeout

Mainly, there are three types of timeout properties that we can play around with:

  • Connection Timeout – The time taken to establish the connection with a remote host.
  • Socket Timeout – This is the time spent waiting for the data after the connection with the remote host has been established. In other words, it is the time between receiving two packets of data.
  • Connection Manager Timeout – Many hosts use a pool of connections to handle concurrent requests. This timeout deals with the time taken to receive an open connection from the pool.

In general, the first two parameters are the most important. However, in high load scenarios, we also need to be careful about properly setting the third timeout as well.

2 – Configuring HTTPClient Timeout Timeouts (the Old Fashioned Way)

Before version 4.3, we had to use normal parameters using generic map to configure HttpClient.

DefaultHttpClient httpClient = new DefaultHttpClient();

int timeout = 5; // seconds
HttpParams httpParams = httpClient.getParams();
httpParams.setParameter(
  CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
httpParams.setParameter(
  CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
httpParams.setParameter(
  ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000));

Here, we have setup the three parameters in the HttpClient object. The timeout is provided in milliseconds and therefore, we multiple the value by 1000. The properties CoreConnectionPNames are part of the org.apache.http package.

3 – Configuring Java HTTPClient Timeout Properties (the New Way)

With version 4.3, we have a much better way of setting the timeout properties. That is by using the HttpClient builder. See the below example for reference:

int timeout = 5;
RequestConfig config = RequestConfig.custom()
  .setConnectTimeout(timeout * 1000)
  .setConnectionRequestTimeout(timeout * 1000)
  .setSocketTimeout(timeout * 1000).build();
CloseableHttpClient httpClient= 
  HttpClientBuilder.create().setDefaultRequestConfig(config).build();

This is a type-safe way to assigning the properties in a clean and error-free way. Therefore, in my view, this is the RECOMMENDED to configure HttpClient timeout properties in your application.

4 – Usage of the HttpClient

After configuring the HttpClient, it is pretty straightforward to use it to make HTTP calls in your application. Example below:

HttpGet getMethod = new HttpGet("http://localhost:8080/path");
HttpResponse response = httpClient.execute(getMethod);
System.out.println(
  "HTTP Status of response: " + response.getStatusLine().getStatusCode());

We can create an HTTP Client object for every request or customize its RequestConfig according to the needs of the application.

However, if we have uniform timeout rules, we can also define a standard HTTPClient object and use it to make HTTP Requests wherever needed. If we are using a Spring-based application, we can very easily declare HttpClient as a bean and inject it using Spring Dependency Injection wherever required.


With this, we have successfully looked at Java HttpClient Timeout Properties and configuring the same for our application. If you have any comments or queries, please do mention in the comments section.

Categories: BlogJava

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *