我有一个使用spring HttpInvokers的客户端服务器应用程序。我在为暂停而挣扎。我看到了关于这个问题的其他线索,但没有明确的答案。
据我所知,readTimeout应该在事务已经连接之后控制它的长度。我已经做了很长时间,因为一些进程,如报告请求,需要时间来运行。这已经有很长一段时间了。
现在的问题是,有时互联网连接失败,或者在请求发出时闪现,而连接却从未建立过。我没有出错,而是建立了一个重试过程,它拦截一个失败的连接并重新尝试它。我希望这些故障能够迅速发生,因此我将HttpClient参数、soTimeout、和soTimeout设置为2秒。
这似乎在很大程度上起了作用。有时候,捕捉问题需要超过2秒的时间,但这是可以的。问题是,soTimeout似乎也适用于或覆盖了readTimout。因此,现在我所有较长时间运行的请求都获得了retry函数,尽管它们连接得很好,只是在等待回复完成。connectionManagerTimeout设置似乎一事无成。似乎没有什么影响实际获得初始连接所需的时间。
是否有另一个应该设置的超时参数?
<bean id="myServiceInterceptor"
class="org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor">
<property name="serviceUrl">
<value>https://myhost/myservices/myMgrService</value>
</property>
<property name="remoteInvocationFactory" ref="remoteInvocationFactory"/>
<property name="httpInvokerRequestExecutor">
<bean class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">
<!--read time out is how long we will wait for a reply. This is large in case we are waiting on a long server process-->
<property name="readTimeout" value="2000000"/>
<!-- we manually set this so we can control the parameter timeout values-->
<property name="httpClient">
<bean class="org.apache.commons.httpclient.HttpClient">
<constructor-arg index="0">
<!--we want multi threaded if we want to ever connect in background threads, this is default if not set anyways in CommonsHttpInvokerRequestExecutor but since we are overriding we have to manually set it or we get the simple one-->
<bean class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager"></bean>
</constructor-arg>
<property name="params">
<bean class="org.apache.commons.httpclient.params.HttpClientParams">
<!--Here we set the socket time out and connection manager time out which is how long we wait for the socket to connect before showing bad feedback.
The default is 60 seconds which makes the client just hang. Now we get immediate response from our RetryConnection-->
<property name="soTimeout" value="2000"/>
<property name="connectionManagerTimeout" value="2000"/>
</bean>
</property>
</bean>
</property>
</bean> </property>
</bean>
<bean id="myServiceMgr" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interceptorNames">
<list>
<value>clientExceptionAdvisor</value>
<value>retryConnection</value>
<value>myServiceInterceptor</value>
</list>
</property>
<property name="proxyInterfaces">
<value>ServiceIF</value>
</property>
</bean>**UPDATE解决方案看起来soTimout也是读取超时。有一种方法可以设置连接超时--这只是一种痛苦。下面是我使用的spring:
<bean class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">
<!--read time out is how long we will wait for a reply. This is large in case we are waiting on a long server process-->
<property name="readTimeout" value="2000000"/>
<!-- we manually set this so we can control the parameter timeout values-->
<property name="httpClient">
<bean class="org.apache.commons.httpclient.HttpClient">
<constructor-arg index="0">
<bean class="org.apache.commons.httpclient.params.HttpClientParams">
<!--Here we set the connection manager time out which is supposed to be how long we wait for the established connection to be returned from the connection manager. This shortness allows us to catch it and enable the retry function.-->
<property name="connectionManagerTimeout" value="2500"/>
</bean>
</constructor-arg>
<constructor-arg index="1">
<!--we want multi threaded if we want to ever connect in background threads, this is default if not set anyways in CommonsHttpInvokerRequestExecutor but since we are overriding we have to manually set it or we get the simple one-->
<bean class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager">
<property name="params">
<bean class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">
<!--Here we set the socket time out which is essentially the same thing as the readTimeout. It is the time we wait for a response-->
<property name="soTimeout" value="2000000"/>
<!--Here we set the connection time out which is supposed to be how long we wait for the connect to be established. This shortness allows us to catch it and enable the retry function. The default is 60 seconds which makes the client just hang. -->
**<property name="connectionTimeout" value="2500"/>**
</bean>
</property>
</bean>
</constructor-arg>
<property name="params">
<bean class="org.apache.commons.httpclient.params.HttpClientParams">
<!--Here we set the socket time out which is essentially the same thing as the readTimeout. It is the time we wait for a response-->
<property name="soTimeout" value="2000000"/>
<!--Here we set the connection manager time out which is how long we wait to get an established connection returned from the connection manager.
The default is 60 seconds which makes the client just hang. Now we get more immediate response from our RetryConnection. But it isn't totally reliable cause of underlying tcp/ip-->
**<property name="connectionManagerTimeout" value="2500"/>**
</bean>
</property>
</bean>
</property>
</bean>发布于 2013-07-12 03:54:30
不能设置最大请求持续时间。这有一些有用的信息。
通常,对于http连接,无论连接类型如何,您都应该具有相同的超时。在不同的时间点有多个滴答数同样的东西是没有意义的。
您应该设置您的API,使报告要么预先运行,要么在不锁定调用方的不同线程上运行。通过这种方式,您可以获得一个status对象,它可以让您知道是否需要再次检查报表。
发布于 2013-07-12 03:59:22
也许我误解了这里的问题,但是在调用sotimeout之前,您不能先检查一下有效的网络连接吗?还是在有有效连接时禁用sotimeout?
对不起,如果这不相关,我有点累了。
发布于 2014-01-07 12:09:18
您可以使用OkHttp并调用
HttpURLConnection connection = okHttpClient.open(new URL(urlStr));
connection.setReadTimeout(60000);https://stackoverflow.com/questions/17606827
复制相似问题