问题描述
我想为我的Web应用程序使用的剩余服务设置连接超时.我正在使用Spring的RestTemplate与我的服务交谈.我做了一些研究,发现并使用了下面的xml(在我的应用程序xml中),我认为这是为了设置超时时间.我正在使用Spring 3.0.
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 to my service. I've done some research and I've found and used the xml below (in my application xml) which I believe is meant to set the timeout. I'm using Spring 3.0.
我在这里也看到了相同的问题 Spring Web服务的超时配置与RestTemplate ,但是解决方案似乎不是干净,我更愿意通过Spring config设置超时值
I've also seen the same problem here Timeout configuration for spring webservices with RestTemplate but the solutions don't seem that clean, I'd prefer to set the timeout values via Spring config
<bean id="RestOperations" class="org.springframework.web.client.RestTemplate">
<constructor-arg>
<bean class="org.springframework.http.client.CommonsClientHttpRequestFactory">
<property name="readTimeout" value="${restURL.connectionTimeout}" />
</bean>
</constructor-arg>
</bean>
无论我将readTimeout设置为什么,都会得到以下信息:
It seems whatever I set the readTimeout to be I get the following:
网络电缆已断开连接:等待约20秒,并报告以下异常:
Network cable disconnected:Waits about 20 seconds and reports following exception:
org.springframework.web.client.ResourceAccessException:I/O错误:主机没有路由:连接;嵌套的异常是java.net.NoRouteToHostException:没有路由到主机:connect
org.springframework.web.client.ResourceAccessExcep tion: I/O error: No route to host: connect; nested exception is java.net.NoRouteToHostException: No route to host: connect
网址不正确,因此REST服务返回了404:等待约10秒,并报告以下异常:
Url incorrect so 404 returned by rest service:Waits about 10 seconds and reports following exception:
org.springframework.web.client.HttpClientErrorException:404未找到
org.springframework.web.client.HttpClientErrorException: 404 Not Found
我的要求要求更短的超时时间,因此我需要能够更改这些超时时间.关于我在做什么错的任何想法吗?
My requirements require shorter timeouts so I need to be able to change these. Any ideas as to what I'm doing wrong?
非常感谢.
推荐答案
我终于可以正常工作了.
I finally got this working.
我认为我们的项目具有commons-httpclient jar的两个不同版本的事实并没有帮助.整理好之后,我发现您可以做两件事...
I think the fact that our project had two different versions of the commons-httpclient jar wasn't helping. Once I sorted that out I found you can do two things...
在代码中,您可以放置以下内容:
In code you can put the following:
HttpComponentsClientHttpRequestFactory rf =
(HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
rf.setReadTimeout(1 * 1000);
rf.setConnectTimeout(1 * 1000);
第一次调用此代码,它将为RestTemplate
使用的HttpComponentsClientHttpRequestFactory
类设置超时.因此,RestTemplate
进行的所有后续调用将使用上面定义的超时设置.
The first time this code is called it will set the timeout for the HttpComponentsClientHttpRequestFactory
class used by the RestTemplate
. Therefore, all subsequent calls made by RestTemplate
will use the timeout settings defined above.
或者更好的选择是这样做:
Or the better option is to do this:
<bean id="RestOperations" class="org.springframework.web.client.RestTemplate">
<constructor-arg>
<bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
<property name="readTimeout" value="${application.urlReadTimeout}" />
<property name="connectTimeout" value="${application.urlConnectionTimeout}" />
</bean>
</constructor-arg>
</bean>
在代码中使用RestOperations
界面的地方,并从属性文件获取超时值.
Where I use the RestOperations
interface in my code and get the timeout values from a properties file.
这篇关于Spring RestTemplate超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!