Configure Timeouts with Spring RestTemplate

In this Spring boot2 RestTemplate timeout example, learn to configure connection timeout and read timeout in Spring RestTemplate with example. 1. Default Timeout By default, RestTemplate uses SimpleClientHttpRequestFactory which depends on the default configuration of HttpURLConnection. Look inside the class source, and you will find this. By default, …

spring boot logo

In this Spring boot2 RestTemplate timeout example, learn to configure connection timeout and read timeout in Spring RestTemplate with example.

1. Default Timeout

By default, RestTemplate uses SimpleClientHttpRequestFactory which depends on the default configuration of HttpURLConnection. Look inside the class source, and you will find this.

private int connectTimeout = -1;
private int readTimeout = -1;

By default, RestTemplate uses the timeout property from JDK installed on the machine which is always infinite if not overridden. To override the default JVM timeout, we can pass these properties during the JVM start. The time unit is in milliseconds.

-Dsun.net.client.defaultConnectTimeout=5000
-Dsun.net.client.defaultReadTimeout=5000

2. Configure Timeout with SimpleClientHttpRequestFactory

To programmatically override the timeout properties, we can customize the SimpleClientHttpRequestFactory class as below.

RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());

//Override timeouts in request factory
private SimpleClientHttpRequestFactory getClientHttpRequestFactory() {

    SimpleClientHttpRequestFactory clientHttpRequestFactory  = new SimpleClientHttpRequestFactory();
    clientHttpRequestFactory.setConnectTimeout(10_000);
    clientHttpRequestFactory.setReadTimeout(10_000);
    return clientHttpRequestFactory;
}

3. Using Apache HttpClient

The SimpleClientHttpRequestFactory helps in setting timeout but it is very limited in functionality and may not prove sufficient in real-time applications. In production code, we may want to use a feature-rich library such as Apache HttpClient.

The HTTPClient library provides other useful features such as a connection pool, idle connection management etc.

Read More : Spring RestTemplate + HttpClient configuration example

RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());

//Override timeouts in request factory
private SimpleClientHttpRequestFactory getClientHttpRequestFactory() {

    HttpComponentsClientHttpRequestFactory clientHttpRequestFactory  = new HttpComponentsClientHttpRequestFactory();
    clientHttpRequestFactory.setConnectTimeout(10_000);
    clientHttpRequestFactory.setReadTimeout(10_000);
    return clientHttpRequestFactory;
}

4. Conclusion

It is very necessary to have timeout property while interacting with remote systems. Any performance issue they cause may hamper the user experience and can bring down the whole application.

In production code, always opt to use HttpClient library. You may consider using SimpleClientHttpRequestFactory only while writing the JUnit tests.

Drop me your questions related to spring boot resttemplate connection timeout example.

Happy Learning !!

Leave a Comment

  1. Hi, can you please help me with the exception I am facing in my Spring Boot Application when trying to connect to external service and waiting for the response. The following exception is thrown after 15 minutes from the time the request is hit to the external service.

    “Operation timed out (Read failed); nested exception is java.net.SocketException: Operation timed out (Read failed)”

    Can setting ReadTimeOut to my RestTemplateBuilder solves the issue?

    Reply
  2. Hi Lokesh, In my application, I have set timeout limit as 200ms for one API call but in rare scenarios I dont get response in 200ms. So, if I increase the timeout limit to 400ms is it going to impact my application overall response in any way?

    Reply
  3. While making delete request, SocketExcption -Unexpected end of file from server is coming while hitting from Web UI app without any delay. If we delay of 2-3 secs then it works fine. Only issue is coming with delete request. We are using Spring boot, Rest template.

    Reply

Leave a Comment

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.