Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestTemplate -- default timeout value

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.

like image 906
saravana_pc Avatar asked Jul 18 '12 08:07

saravana_pc


People also ask

What is a good timeout value?

Your timeouts should be about 3 seconds.

What is default connection timeout in Spring boot?

connection-timeout=5000 in your application.

What is REST API timeout?

The REST client timeouts when a REST call takes longer to respond than the specified time. When this happens, the OUT event fails. The default value of the REST client response timeout is 120 seconds. You can increase this time if an adapter that you use has longer than normal response times.


2 Answers

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  
like image 63
nevster Avatar answered Oct 08 '22 11:10

nevster


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.

like image 30
Jigar Parekh Avatar answered Oct 08 '22 09:10

Jigar Parekh