【问题标题】:Timeout configuration for spring webservices with RestTemplate使用 RestTemplate 对 Spring Web 服务进行超时配置
【发布时间】:2012-02-27 19:56:19
【问题描述】:

我想使用 RestTemplate 在客户端为 Spring Web 服务配置超时。我尝试了以下配置:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg>
    <bean class="org.springframework.http.client.CommonsClientHttpRequestFactory">
    <property name="readTimeout" value="10000" />
    </bean>
</constructor-arg>
    <property name="messageConverters">
    <list>
    <ref bean="stringHttpMessageConverter" />
    <ref bean="marshallingHttpMessageConverter" />
    </list>
    </property>
</bean>

但是当我启动我的 tomcat 时出现 NoClassDefFoundError :

06 févr. 2012 10:43:43,113 [ERROR,ContextLoader] Context initialization failed
java.lang.NoClassDefFoundError: org/apache/commons/httpclient/HttpMethodBase

但是我在 pom.xml 中包含了 commons-httpclient :

    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
    </dependency

知道我该怎么做/解决这个问题吗?

提前致谢!

【问题讨论】:

    标签: java web-services spring timeout resttemplate


    【解决方案1】:

    Snicolas 的回答几乎对我有用,只需要更改演员阵容:

    RestTemplate restTemplate = new RestTemplate();    
    ((SimpleClientHttpRequestFactory)restTemplate.getRequestFactory()).setReadTimeout(1000*30);
    

    你也可以设置连接超时:

    ((SimpleClientHttpRequestFactory)restTemplate.getRequestFactory()).setConnectTimeout(1000*30);
    

    【讨论】:

      【解决方案2】:

      这对我有用

      ( (HttpComponentsClientHttpRequestFactory) getRestTemplate().getRequestFactory() ).setReadTimeout( 120 * 1000 );
      

      我在android版spring android rest模板中使用过。


      默认值为 60 * 1000

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题,首先尝试通过修改Spring配置来解决它 但我的尝试都没有成功。

        最后,我通过设置以下 JVM 系统属性部分修复了它: sun.net.client.defaultConnectTimeout

        sun.net.client.defaultReadTimeout

        (点击该链接了解更多详情:http://docs.oracle.com/javase/1.4.2/docs/guide/net/properties.html

        首先,我为“连接超时”注入我的自定义值 和“读取超时”存储在属性文件中,使用“自制”配置 bean:

           <bean id="rmProperties"  class="com.mydomain.myapp.myConfigBean" scope="singleton">
            ...
            <property name="httpRequestConnectTimeout" value="${httpRequestConnectTimeout}" />
            <property name="httpRequestReadTimeout" value="${httpRequestReadTimeout}" />
            ...
            </bean>
        

        然后,我使用 System.setProperty(...) 方法设置 JVM 系统属性,如下所示:

            System.setProperty(propName, value);
        

        我只剩下一个麻烦:sun.net.client.defaultConnectTimeout 中设置的值 似乎没有考虑在内。 在进行了更多测试之后,我意识到当我尝试通过代理服务器(在我的例子中是 Squid)访问我的目标主机时会发生这种情况。

        但是,使用该设置方法有一个不便:超时设置将用于所有进一步的请求

        问候

        【讨论】:

          【解决方案4】:

          我同样需要能够为 web 服务消费设置超时,我只是用另一个 spring conf 来解决它。

          首先使用下面的配置,我遇到了与@jsebFrank (java.lang.NoClassDefFoundError: org/apache/commons/httpclient/HttpMethodBase) 相同的问题

              <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
              <constructor-arg>
                  <bean
                      class="org.springframework.http.client.CommonsClientHttpRequestFactory">
                      <property name="connectTimeout" value="10000" />
                      <property name="readTimeout" value="10000" />
                  </bean>
              </constructor-arg>
              </bean>
          

          但是正如 Spring 支持解释 here(在第 16.5 节超时处理中),您可以使用 SimpleClientHttpRequestFactory 请求工厂(这是 Spring restTemplate 的默认工厂)。

          使用它,我没有问题了:

              <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
              <constructor-arg>
                  <bean
                      class="org.springframework.http.client.SimpleClientHttpRequestFactory">
                      <property name="connectTimeout" value="10000" />
                      <property name="readTimeout" value="10000" />
                  </bean>
              </constructor-arg>
              </bean>
          

          【讨论】:

            猜你喜欢
            • 2019-06-05
            • 2017-07-08
            • 2012-11-29
            • 1970-01-01
            • 2016-09-07
            • 2020-10-10
            • 2015-10-22
            • 1970-01-01
            相关资源
            最近更新 更多