Support for Request timeout in spring rest template
See original GitHub issueI use spring rest template for performing a head request with a proxy server. The target host does not exist and the proxy just does not finish the request. The problem is, that neither the connect timeout nor the read timeout stop the processing.
My code:
final ResponseEntity<String> sourceRes = restTemplate.exchange(head(new URI(post.getSourceUrl())).build(), String.class);
return sourceRes.getStatusCode().is2xxSuccessful();
Here is my code for setting up the resttemplate:
final HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder.useSystemProperties();
clientBuilder.setDefaultRequestConfig(custom()
.setCookieSpec(STANDARD)
.setConnectTimeout(Duration.ofSeconds(1).toMillis())
.setSocketTimeout(Duration.ofSeconds(1).toMillis())
.build());
final HttpClient httpClient = clientBuilder.build();
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
requestFactory.setConnectTimeout(Duration.ofSeconds(1).toMillis());
requestFactory.setReadTimeout(Duration.ofSeconds(1).toMillis());
final RestTemplate restTemplate = new RestTemplate(requestFactory);
Here is the request performed with curl:
curl -X HEAD --proxy "proxyuser:proxypassword@proxyhost:1234t" "https://iAmNotExisting.com/dummy.jpg" --verbose
* About to connect() to proxy proxyhost port (#0)
* Trying 15.112.12.32...
* Connected to proxyhost (15.112.12.32) port 1234 (#0)
* Establish HTTP proxy tunnel to iAmNotExisting.com:443
* Proxy auth using Basic with user 'proxyuser'
> CONNECT iAmNotExisting.com:443 HTTP/1.1
> Host: iAmNotExisting.com:443
> Proxy-Authorization: Basic blablub=
> User-Agent: curl/7.29.0
> Proxy-Connection: Keep-Alive
>
* Operation timed out after 300314 milliseconds with 0 out of 0 bytes received
* Closing connection 0
curl: (28) Operation timed out after 300314 milliseconds with 0 out of 0 bytes received
As you can see, curl sends the request headers, but does not receive any response.
I think, this is either a bug, because the socket timeout does not work or that a request timeout would be needed if the socket timeout is not intended for this use case
The behavior of the proxy could be caused, because it is a keep-alive session.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:8 (6 by maintainers)
Top Results From Across the Web
rest - Spring RestTemplate timeout - Stack Overflow
I would like to set the connection timeouts for a rest service used by my web application. I'm using Spring's RestTemplate to talk...
Read more >Configuring Timeouts with Spring RestTemplate
By default, RestTemplate uses SimpleClientHttpRequestFactory which depends on default configuration of HttpURLConnection . Look inside the class ...
Read more >Setting a Request Timeout for a Spring REST API - Baeldung
One way we can implement a request timeout on database calls is to take advantage of Spring's @Transactional annotation. It has a timeout ......
Read more >Spring RestTemplate — why the set timeout does not work
Consider a simple application whose purpose is to call one endpoint several times and record the duration of requests. Of course, you have...
Read more >Spring RestTemplate Timeout - HelloKoding
Connection vs Read Timeout · Configure RestTemplate timeout · Test a Connect timeout · Test a Read timeout · Run the tests ·...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The default socket config is set on the
HttpClientitself; in our comments, we’re using the default socket config but we can also configure that through the connection manager, and much more. I believe this goes hand in hand with the SSL configuration - which is available only on theHttpClientbuilder itself.RestTemplateBuilderis definitely not at the right abstraction level - and even Spring Framework’sHttpComponentsClientHttpRequestFactoryonly takesHttpClientas an immutable constructor argument (at that point it’s too late).I’ve created SPR-16697 to at least provide a hint for developers using the {{setConnectTimeout}} method on the request factory. Hopefully this will save them some time if they’re facing the same issue.
Thanks for the report!
SPR-16697 was fixed in Spring Framework 5.0.5 and was not backported to 4.3.x. Even if it was, this is just a documentation issue as we can’t apply that timeout at the request level but this needs to be done on the ˋHttpClient` instance itself. So this is consistent with what you’re seeing right now.