Saturday, 18 April 2020

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.
RestTemplate default timeout
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.
Override default timeout in JVM
-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.
Override timeout with SimpleClientHttpRequestFactory
//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.
Override timeout with HttpComponentsClientHttpRequestFactory
//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.

No comments:

Post a Comment