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.

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 !!

Comments

Subscribe
Notify of
guest
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

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.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode