Method of setting timeout duration in Resttemplate

  • 2021-12-04 18:53:11
  • OfStack

Directory Resttemplate Set timeout RestTemplate Set timeout attention points

Resttemplate Set timeout

In order to meet the call requirements, it is necessary to modify the timeout when sending requests using Resttemplate. The relevant modification methods are given on the Internet, and the codes are as follows:


HttpComponentsClientHttpRequestFactory rf =
            ((HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory());
        rf.setConnectTimeout(30000);
        rf.setReadTimeout(30000);

But an error is reported at run time:

org.springframework.http.client.InterceptingClientHttpRequestFactory cannot be cast to org.springframework.http.client.HttpComponentsClientHttpRequestFactory

The type returned by restTemplate. getRequestFactory () cannot be converted. Through searching and debugging, it is found that in resttemplate, timeout must be set before setting interceptor, because timeout cannot be set after setting interceptor, and I have not set interceptor in setting, so it should be constructed by default

So the solution is as follows:


restTemplate.setInterceptors(null);
HttpComponentsClientHttpRequestFactory rf =
            ((HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory());
rf.setConnectTimeout(30000);
rf.setReadTimeout(30000);

Before setting the timeout, leave the interceptor empty so that there is no problem that it cannot be set, and then verify it.

After looking at the source code of resttemplate, it is found that this situation occurs because there is such a piece of code in the source code of resttemplate:


/**
 * Return the request interceptor that this accessor uses.
 */
public List<ClientHttpRequestInterceptor> getInterceptors() {
   return interceptors;
}
@Override
    public ClientHttpRequestFactory getRequestFactory() {
        ClientHttpRequestFactory delegate = super.getRequestFactory();
        if (!CollectionUtils.isEmpty(getInterceptors())) {
            return new InterceptingClientHttpRequestFactory(delegate, getInterceptors());
        }
        else {
            return delegate;
        }
    }

As you can see, in the getRequestFactory () function, it should have returned ClientHttpRequestFactory type, but it will check whether it contains interceptors interceptor once first. If the interceptor is not empty, it will return InterceptingClientHttpRequestFactory containing interceptor, which cannot be converted to HttpComponentsClientHttpRequestFactory type. Therefore, the interceptor will be left empty in the previous processing, so that the required object without interceptor can be returned.

RestTemplate Setting timeout attention points

1. Ensure that there is only one RestTemplate configuration in the system; Otherwise, it may not be consistent with your expectations.

2. Never trust the code you write too much; Print more logs to really know the call time;


long s = System.currentTimeMillis();
        try {
            responseEntity = restTemplate.exchange();
        } catch (Exception e) {
            long costTime = System.currentTimeMillis()-s;
            log.error(" Call ** Service exception , Spend time :{}, Errors :{}",costTime, e.getMessage(), e);
        }

Related articles: