Spring rest template readTimeOut
Asked Answered
S

4

14

I'm trying to understand the readTimeout available on restTemplate, what is it exactly ?

Is it the the total amount of time the request can take before we get the timeout exception ?

Salientian answered 16/8, 2017 at 12:38 Comment(0)
A
18

You can define a read timeout on a RestTemplate as follows:

HttpComponentsClientHttpRequestFactory clientRequestFactory = new HttpComponentsClientHttpRequestFactory();
// set the read timeout, this value is in milliseconds
clientRequestFactory.setReadTimeout(500);

RestTemplate restTemplate = new RestTemplate(clientRequestFactory);

Given a readTimeout of X millis, any request made through that RestTemplate instance which takes longer than X millis will result in a ResourceAccessException, wrapping a java.net.SocketTimeoutException with the exception message: "Read timed out".

The timeout is actually implemented by the socket connector inside the HttpClient instance which is wrapped by the RestTemplate so the clock starts when the request first hits that socket and stops when whichever of these comes first: the request completes or the readTimeout is reached.

In effect this means that any request which takes longer than the configured readTimeout will fail with a timeout exception.

Alvaalvan answered 16/8, 2017 at 13:57 Comment(1)
Beware that readTimeout doesn't guaratee complete response arrival time limit: understanding URLConnection.setReadTimeout()Rechabite
G
50

As far as i knew, In restTemplate we have 3 type of timeouts

  1. ConnectionRequestTimeout. This is timeout in millis for getting connection from connectionManager

  2. ConnectionTimeout. This is timeout in millis for establishing connection between source and destination

  3. ReadTimeout. This is timeout in millis which expects the response/result should be returned from the destination endpoint.

Goy answered 16/8, 2017 at 14:31 Comment(4)
This should really be the selected answer as it literally answered the OP's question in a neat way.Monkery
ReadTimeout is incorrect, read timeout occurs when time between receiveing two parts of data from server is greater than timeout value. You suggest that it's total time of response, which is not true: total time might be greater than timeout valueJakob
@Jakob is there any way i restrict total time, say 1 min, to receive complete response.Actomyosin
@DhruvamGupta outside of RestTemplate, for example iteratrlearning.com/java9/2016/09/13/…Degenerate
A
18

You can define a read timeout on a RestTemplate as follows:

HttpComponentsClientHttpRequestFactory clientRequestFactory = new HttpComponentsClientHttpRequestFactory();
// set the read timeout, this value is in milliseconds
clientRequestFactory.setReadTimeout(500);

RestTemplate restTemplate = new RestTemplate(clientRequestFactory);

Given a readTimeout of X millis, any request made through that RestTemplate instance which takes longer than X millis will result in a ResourceAccessException, wrapping a java.net.SocketTimeoutException with the exception message: "Read timed out".

The timeout is actually implemented by the socket connector inside the HttpClient instance which is wrapped by the RestTemplate so the clock starts when the request first hits that socket and stops when whichever of these comes first: the request completes or the readTimeout is reached.

In effect this means that any request which takes longer than the configured readTimeout will fail with a timeout exception.

Alvaalvan answered 16/8, 2017 at 13:57 Comment(1)
Beware that readTimeout doesn't guaratee complete response arrival time limit: understanding URLConnection.setReadTimeout()Rechabite
P
11

You can also define a bean:

@Bean
public RestTemplate restTemplateReadTimeout(RestTemplateBuilder builder) {
    return builder
            .setReadTimeout(15000) //15 seconds
            .build();
}

And use it:

@Autowired
@Qualifier("restTemplateReadTimeout")
private RestTemplate restTemplate;

PS.: When I used this configuration on Spring Boot, I tried to create different RestTemplate Beans with different timeout configurations. But I ended up seeing Spring using always only one timeout configuration (probably using the timeout from the last bean registered), acting as the timeout configuration was a Singleton among the RestTemplates. So pay attention on that, I don't know if was some mistake on my configuration, bug or expected behavior.

Ptah answered 25/6, 2018 at 15:56 Comment(0)
Z
5

I know this isn't the OP question, but I suspect a lot of people (like myself) end up here trying to find how to set the read timeout, not just what it is.

The existing 2 code examples no longer work in Spring Boot 3 / Spring Framework 6, so this was the easiest I found:

// Using the builder pattern
restTemplate = new RestTemplateBuilder().setReadTimeout(Duration.ofSeconds(10)).build();

It is technically still possible to configure the connection factory directly, but it's a bit more verbose than it was in prior Spring versions:

To change the socket read timeout, use SocketConfig.Builder.setSoTimeout(Timeout), supply the resulting SocketConfig to org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder.setDefaultSocketConfig(SocketConfig), use the resulting connection manager for org.apache.hc.client5.http.impl.classic.HttpClientBuilder.setConnectionManager(HttpClientConnectionManager), and supply the built HttpClient to HttpComponentsClientHttpRequestFactory(HttpClient).

Zannini answered 28/4, 2023 at 18:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.