RestTemplate — default timeout value

前端 未结 4 1475
礼貌的吻别
礼貌的吻别 2020-12-25 11:13

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

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

RestTemplate restT         


        
相关标签:
4条回答
  • 2020-12-25 11:57

    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 
    
    0 讨论(0)
  • 2020-12-25 11:59

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-25 12:08

    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.

    0 讨论(0)
  • 2020-12-25 12:08

    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());
    
    0 讨论(0)
提交回复
热议问题