HttpClient超时 [英] HttpClient timeout

查看:109
本文介绍了HttpClient超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Spring HttpInvokers的客户端服务器应用程序.我正在努力应对超时问题.我看到与此相关的其他主题,但没有明确的答案.

I have a client server app using spring HttpInvokers. I am struggling with the timeouts. I see other threads regarding this but no clear answers.

据我所知,readTimeout应该控制事务已连接后的长度.我花了很长的时间,因为某些过程(例如报告请求)需要一些时间才能运行.现在,这种方法已经运行了很长一段时间.

As far as I know the readTimeout is supposed to control the length of a transaction after it has already connected. I have made this very long because some processes such as report requests take time to run. This has worked fine for a long period of time now.

现在的问题是,有时互联网连接会失败,或者在发出请求时就突然消失,而从不建立连接.我没有发出错误,而是建立了重试过程,该过程将拦截失败的连接并重试.我希望这些故障能够快速发生,因此我将HttpClient参数 soTimeout connectionManagerTimeout 设置为2秒.

Now the problem is that sometimes the internet connection fails or blips out at the moment a request is made and the connection is never made. Instead of erroring out I have instituted a retry procedure which intercepts a failed connection and retries it. I want these failures to happen quickly so I set the HttpClient parameters soTimeout and connectionManagerTimeout to 2 seconds.

这似乎在大多数情况下都是有效的.有时需要花费超过2秒钟的时间才能解决问题,但这没关系.问题是soTimeout似乎也适用于或覆盖readTimout.因此,现在我所有运行时间较长的请求都获得了重试功能,即使它们连接得很好,也只是在等待答复完成. connectionManagerTimeout设置似乎根本不执行任何操作.似乎没有任何影响实际获得初始连接所花费的时间.

This seemed to work for the most part. Occasionally it takes longer than 2 seconds to catch the problem but that is ok. The issue is that the soTimeout seems to apply to or override the readTimout as well. So now all my longer running requests get the retry function even though they have connected just fine and are simply waiting for the reply to complete. The connectionManagerTimeout setting seems to do nothing at all. Nothing seems to affect the time it takes to actually acquire the initial connection.

我应该设置另一个超时参数吗?

Is there another timeout parameter I should be setting?

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

* *更新解决方案 看起来soTimout也是读取超时.有一种方法可以设置连接超时,这只是一个痛苦.这是我使用的spring xml:

**UPDATE SOLUTION Looks like soTimout is read timeout also. There is a way to set connection timeout it is just a pain. Here is the spring xml i used:

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

推荐答案

您不能设置最大请求持续时间. 具有一些有用的信息.

You cannot set a maximum request duration. This has some helpful info.

通常,对于http连接,无论连接类型如何,都应具有相同的超时时间.有多个滴答声会在不同点上计数同一事物是没有意义的.

Generally with http connections, you should have the same timeout regardless of connection type. It makes no sense to have multiple ticks counting off the same thing at different points.

您应该将API设置为预先运行报告,或者在不会锁定调用程序的其他线程上运行报告.这样,您可以取回状态对象,该对象使您知道是否需要再次检查报告.

You should set up your API to either have the reports either pre-run, or run on a different thread that's doesn't lock up your invoker. This way you can get back a status object that lets you know if you need to check again for a report.

这篇关于HttpClient超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆