本文介绍了春季休息模板 readTimeOut的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 restTemplate 上可用的 readTimeout,它到底是什么?

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 ?

推荐答案

您可以在 RestTemplate 上定义读取超时,如下所示:

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);

给定 X 毫秒的 readTimeout,任何通过该 RestTemplate 实例发出的时间超过 X 毫秒的请求都将导致 ResourceAccessException,用异常消息包装 java.net.SocketTimeoutException:读取超时".

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".

超时实际上是由 HttpClient 实例内的套接字连接器实现的,该实例由 RestTemplate 包装,因此时钟在请求第一次命中该套接字时开始,并在以先到者为准:请求完成或达到 readTimeout.

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.

实际上,这意味着任何比配置的 readTimeout 花费时间更长的请求都将失败并出现超时异常.

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

这篇关于春季休息模板 readTimeOut的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 07:52