RestTemplate -- default timeout value
Asked Answered
T

4

45

What is the default timeout value when using Spring's RestTemplate?

For e.g., I am invoking a web service like this:

RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject("http://webservice.com/item/3455", String.class);

Is there any built-in timeout value for RestTemplate? I am not planning to change the timeout value, however, I want to ensure that there is a reasonable timeout for every request.

Typesetter answered 18/7, 2012 at 8:50 Comment(0)
D
26

I think you can use SimpleClientHttpRequestFactory for timeout parameter. Instance of SimpleClientHttpRequestFactory can be set to rest template by constructor or setter method.

By default RestTemplate uses SimpleClientHttpRequestFactory so may be you can directly set value to restTemplate.

Despain answered 18/7, 2012 at 9:14 Comment(4)
thanks. I checked the documentation of SimpleClientHttpRequestFactory and it mentions that "the default timeout is the system's default timeout". What does that mean ?Typesetter
i have looked at source code for SimpleClientHttpRequestFactory default value is -1 for both read and connect timeout. i think default they meant by as HTTP is dependent on URLConnection it will depend on jdk for default timeout settingDespain
The default timeout for URLConnection is infinite. Does that mean RestTemplate also has an infinite timeout value?Typesetter
yes by default, have a look link. you can set system timeout using system propertiesDespain
P
39

To explicitly answer the question...

The default timeout is infinite.

By default RestTemplate uses SimpleClientHttpRequestFactory and that in turn uses HttpURLConnection.

By default the timeout for HttpURLConnection is 0 - ie infinite, unless it has been set by these properties :

-Dsun.net.client.defaultConnectTimeout=TimeoutInMiliSec 
-Dsun.net.client.defaultReadTimeout=TimeoutInMiliSec 
Pierides answered 12/5, 2016 at 1:38 Comment(2)
Is there an api to query the currently set values? I can see the setters on HttpComponentsClientHttpRequestFactory to set the values, but there's no corresponding getters to query the values directly?Cabanatuan
This is the only thing which worked for me. I tried to set the timeout via API for HttpComponentsClientHttpRequestFactory but didn't work. I had to pass the above values as runtime parameters (Spring Boot - 2.1.5)Breban
D
26

I think you can use SimpleClientHttpRequestFactory for timeout parameter. Instance of SimpleClientHttpRequestFactory can be set to rest template by constructor or setter method.

By default RestTemplate uses SimpleClientHttpRequestFactory so may be you can directly set value to restTemplate.

Despain answered 18/7, 2012 at 9:14 Comment(4)
thanks. I checked the documentation of SimpleClientHttpRequestFactory and it mentions that "the default timeout is the system's default timeout". What does that mean ?Typesetter
i have looked at source code for SimpleClientHttpRequestFactory default value is -1 for both read and connect timeout. i think default they meant by as HTTP is dependent on URLConnection it will depend on jdk for default timeout settingDespain
The default timeout for URLConnection is infinite. Does that mean RestTemplate also has an infinite timeout value?Typesetter
yes by default, have a look link. you can set system timeout using system propertiesDespain
W
12

One of the nice features of spring-android RestTemplate is the use of appropriate (recommended by Google) implementation of RequestFactory depending on the version of OS.

Google recommends to use the J2SE facilities on Gingerbread (Version 2.3) and newer, while previous versions should use the HttpComponents HttpClient. Based on this recommendation RestTemplate checks the version of Android on which your app is running and uses the appropriate ClientHttpRequestFactory.

So the previous answer is not full because HttpComponentsClientHttpRequestFactory (which is used by spring-android for Android OS versions < 2.3) is not taken into consideration.

My solution was something like this:

public class MyRestTemplate extends RestTemplate {
    public MyRestTemplate() {
        if (getRequestFactory() instanceof SimpleClientHttpRequestFactory) {
            Log.d("HTTP", "HttpUrlConnection is used");
            ((SimpleClientHttpRequestFactory) getRequestFactory()).setConnectTimeout(10 * 1000);
            ((SimpleClientHttpRequestFactory) getRequestFactory()).setReadTimeout(10 * 1000);
        } else if (getRequestFactory() instanceof HttpComponentsClientHttpRequestFactory) {
            Log.d("HTTP", "HttpClient is used");
            ((HttpComponentsClientHttpRequestFactory) getRequestFactory()).setReadTimeout(10 * 1000);
            ((HttpComponentsClientHttpRequestFactory) getRequestFactory()).setConnectTimeout(10 * 1000);
        }
    }
}
Wound answered 14/1, 2013 at 14:49 Comment(3)
Thanks for the code! How can we set a timeout message for the rest calls? Because I want to display some message when the requests is timedout with no result and no error from the server sideCanso
when you perform the request, catch the SocketTimeoutException, or smth similar - and show your timeout message.Wound
Thanks for the comment! :) I am handling too many rest calls which I am combing in a RestService interface using android annotations. All those rest calls throws RestClientException. But when I added IOException under which comes SocketTimeoutException, it is asking me to handle it everywhere the rest calls are made. Is there anyway to handle this without making change in each rest call? I have posted my Q: over here : https://mcmap.net/q/374337/-handle-connection-and-read-timeouts-for-restclient-calls-in-android/1237656Canso
C
11

You can use ClientHttpRequestFactory param in the RestTemplate constructor:

private final int HTTP_CONNECT_TIMEOUT = 15000;
private final int HTTP_READ_TIMEOUT = 10000;

private ClientHttpRequestFactory getClientHttpRequestFactory() {
    HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
    clientHttpRequestFactory.setConnectTimeout(HTTP_CONNECT_TIMEOUT);
    clientHttpRequestFactory.setReadTimeout(HTTP_READ_TIMEOUT);
    return clientHttpRequestFactory;
}

When you need a new RestTemplate, create it like this example:

RestTemplate rt = new RestTemplate(getClientHttpRequestFactory());
Chigger answered 29/4, 2019 at 11:2 Comment(1)
The question is "What's the default timeout", not how to set a timeout value.Lundeen

© 2022 - 2024 — McMap. All rights reserved.