【问题标题】:CXF java.net.ConnectException: Connection timed outCXF java.net.ConnectException:连接超时
【发布时间】:2013-06-22 07:28:03
【问题描述】:

当我尝试从 WS 客户端调用我已部署的 CXF Web 服务的方法时,连接超时。两者都使用自定义拦截器,服务因多次调用而过载。

Caused by: java.net.ConnectException: ConnectException invoking http://xxx.xx.xx.xx:12005/myservice/repository?wsdl: Connection timed out
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.mapException(HTTPConduit.java:1338)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1322)
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:622)
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
    ... 36 more

我尝试了多种解决方案来禁用超时或增加超时,但都失败了。

首先,我尝试创建一个 CXF 配置文件,如下所示:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
    xsi:schemaLocation="http://cxf.apache.org/transports/http/configuration
           http://cxf.apache.org/schemas/configuration/http-conf.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <http-conf:conduit name="*.http-conduit">
        <http-conf:client CacheControl="no-cache"
            ConnectionTimeout="0" ReceiveTimeout="0" AllowChunking="false" />
    </http-conf:conduit>
</beans>

然后,我通过使用 Java 系统属性 -Dcxf.config.file=/home/test/resources/cxf.xml 强制我的应用程序加载它

在日志中我可以看到配置已被读取,因此可能已应用

INFO:已加载配置文件 /home/test/resources/cxf.xml。

不幸的是,连接超时仍然发生。

我尝试的第二种解决方案包括使用以下代码以编程方式在所有客户端上设置策略:

public static void setHTTPPolicy(Client client) {
    HTTPConduit http = (HTTPConduit) client.getConduit();

    HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
    httpClientPolicy.setConnectionTimeout(0);
    httpClientPolicy.setReceiveTimeout(0);
    httpClientPolicy.setAsyncExecuteTimeout(0);

    http.setClient(httpClientPolicy);
}

但再次发生连接超时。

我错过了什么吗?是否有其他一些超时配置?欢迎任何帮助。

【问题讨论】:

  • 连接超时通常是另一个问题的征兆,而不是其本身的原因。你是说对于服务的某些调用,连接不会超时?
  • 是的,一些调用运行良好。我的场景包括传输大附件,因此它解释了超时。我遇到的问题实际上是客户端和服务器端的超时配置。
  • 啊。然后我建议使用线程池。很快在下面找到我的答案。

标签: web-services cxf soap-client connection-timeout


【解决方案1】:

CXF 允许您为 Web 服务端点配置线程池。这样,您可以应对由于请求处理资源稀缺而导致的超时。下面是使用 cxf 中的&lt;jaxws:endpoint/&gt; 选项的示例配置:

<jaxws:endpoint id="serviceBean"  implementor="#referenceToServiceBeanDefinition" address="/MyEndpointAddress">
        <jaxws:executor>
             <bean id="threadPool" class="java.util.concurrent.ThreadPoolExecutor">  
                 <!-- Minimum number of waiting threads in the pool -->
                 <constructor-arg index="0" value="2"/>
                 <!-- Maximum number of working threads in the pool -->
                 <constructor-arg index="1" value="5"/>
                 <!-- Maximum wait time for a thread to complete execution -->
                 <constructor-arg index="2" value="400000"/>
                 <!-- Unit of wait time -->
                 <constructor-arg index="3" value="#{T(java.util.concurrent.TimeUnit).MILLISECONDS}"/>
                 <!-- Storage data structure for waiting thread tasks -->
                 <constructor-arg index="4" ref="taskQueue"/>
             </bean>
         </jaxws:executor>
    </jaxws:endpoint>

    <!-- Basic data structure to temporarily hold waiting tasks-->
    <bean id="taskQueue" class="java.util.concurrent.LinkedBlockingQueue"/>

【讨论】:

    猜你喜欢
    • 2011-08-05
    • 2014-03-01
    • 2015-09-27
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多