Spring RestTemplate timeout configuration example

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

1. RestTemplate default timeout

By default, RestTemplate uses SimpleClientHttpRequestFactory which depends on 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 timeout property from JDK installed on the machine which is always infinite in not overridden. To override the default JVM timeout, we can pass these properties during JVM start.

-Dsun.net.client.defaultConnectTimeout=<TimeoutInMiliSec>
-Dsun.net.client.defaultReadTimeout=<TimeoutInMiliSec>

2. RestTemplate timeout with SimpleClientHttpRequestFactory

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

//Create resttemplate
RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());

//Override timeouts in request factory
private SimpleClientHttpRequestFactory getClientHttpRequestFactory() 
{
    SimpleClientHttpRequestFactory clientHttpRequestFactory
                      = new SimpleClientHttpRequestFactory();
    //Connect timeout
    clientHttpRequestFactory.setConnectTimeout(10_000);
    
    //Read timeout
    clientHttpRequestFactory.setReadTimeout(10_000);
    return clientHttpRequestFactory;
}

3. RestTemplate timeout with HttpComponentsClientHttpRequestFactory

SimpleClientHttpRequestFactory helps in setting timeout but it is very limited in functionality and may not prove sufficient in realtime applications. In production code, we may want to use HttpComponentsClientHttpRequestFactory which support HTTP Client library along with resttemplate.

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

Read More : Spring RestTemplate + HttpClient configuration example

//Create resttemplate
RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());

//Override timeouts in request factory
private SimpleClientHttpRequestFactory getClientHttpRequestFactory() 
{
    HttpComponentsClientHttpRequestFactory clientHttpRequestFactory
                      = new HttpComponentsClientHttpRequestFactory();
    //Connect timeout
    clientHttpRequestFactory.setConnectTimeout(10_000);
    
    //Read timeout
    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 !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

4 thoughts on “Spring RestTemplate timeout configuration example”

  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?

  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?

  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.

Comments are closed.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.