`
lg_aku
  • 浏览: 48032 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
文章分类
社区版块
存档分类
最新评论

CXF客户端配置请求超时限制-SocketTimeoutException

阅读更多

在用cxf  开发webservice应用时,客户端老报

java.net.SocketTimeoutException: Read timed out

原因为连接超时,google 参考链接

Spring下设置CXF的WebService客户端超时时长
http://ajava.org/course/open/16514.html
https://cwiki.apache.org/CXF20DOC/client-http-transport-including-ssl-support.html   (官方)



在Spring+CXF的WebService环境下,客户端有两个时间属性是可配置的,分别是ConnectionTimeout和ReceiveTimeout.  
ConnectionTimeout--WebService以TCP连接为基础,这个属性可以理解为tcp的握手时的时间设置,超过设置的时间长则认为是连接超时.以毫秒为单位,默认是30000毫秒,即30秒. 
 
ReceiveTimeout -- 这个属性是发送WebService的请求后等待响应的时间,超过设置的时长就认为是响应超时.以毫秒为单位,默认是60000毫秒,即60秒.

   一、在spring的配置文件中进行设置(客户端)。

Xml代码 
<?xml version="1.0" encoding="UTF-8"?>    
<beans xmlns="http://www.springframework.org/schema/beans"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
    xmlns:jee="http://www.springframework.org/schema/jee"    
    xmlns:jaxws="http://cxf.apache.org/jaxws"    
    xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"     
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd    
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd    
           http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd    
           http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd ">    
        <http-conf:conduit name="{http://impl.service.product.super.com/}ProjectService.http-conduit">     
        <http-conf:client ConnectionTimeout="10000" ReceiveTimeout="20000"/>    
    </http-conf:conduit>     
</beans>   

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd 
           http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 
           http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd "> 
        <http-conf:conduit name="{http://impl.service.product.super.com/}ProjectService.http-conduit">  
        <http-conf:client ConnectionTimeout="10000" ReceiveTimeout="20000"/> 
    </http-conf:conduit>  
</beans>  
这里需要注意的有几个地方:  
需要指定http-conf名称空间 xmlns:http-conf =http://cxf.apache.org/transports/http/configuration  
指定模式位置:  http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd   
http-conf:conduit中的name属性,指定设置生效的服务,如例子中,只对服务名为{http://impl.service.product.sww.com/}ProjectService的服务生效.  
使用下面的设置则会对所有服务生效  
< http-conf:conduit   name = "*.http-conduit" >         
</ http-conf:conduit >  

二、通过java代码进行设置。

Java代码 
Client client = ClientProxy.getClient(port);     
HTTPConduit http = (HTTPConduit) client.getConduit();     
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();     
httpClientPolicy.setConnectionTimeout(36000);     
httpClientPolicy.setAllowChunking(false);     
httpClientPolicy.setReceiveTimeout(32000);     
http.setClient(httpClientPolicy);   

    Client client = ClientProxy.getClient(port);  
    HTTPConduit http = (HTTPConduit) client.getConduit();  
    HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();  
    httpClientPolicy.setConnectionTimeout(36000);  
    httpClientPolicy.setAllowChunking(false);  
    httpClientPolicy.setReceiveTimeout(32000);  
    http.setClient(httpClientPolicy); 
另外,WSDL中的endpoint的地址不一定是有效的,为避免客户端请求使用该地址,我们在请求前应通过以下方式强行设置为可用的服务地址。
  ((BindingProvider) port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,serviceUrl);

分享到:
评论
1 楼 tangduDream 2012-01-18  
我想问你一个问题,为什么我配置:
//超时设置
Client clientP = ClientProxy.getClient(companyService);
HTTPConduit http = (HTTPConduit) clientP.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
         //httpClientPolicy.setConnectionTimeout(20000);
         httpClientPolicy.setReceiveTimeout(5*60*1000);
         //httpClientPolicy.setAllowChunking(false);
         http.setClient(httpClientPolicy);
我设置了5分钟 ,可是服务端却在5分钟内还在运行,直到超时?

这是为什么呢?

相关推荐

    cxf客户端所需jar包-4个

    2. **cxf-rt-frontend-jaxws.jar**:这是CXF的Java JAX-WS(Java API for XML Web Services)前端模块。JAX-WS是Java平台上的标准,用于创建和消费基于SOAP的Web服务。这个库提供了客户端和服务器端的实现,包括编...

    cxf-rt-transports-http-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-transports-http-3.0.1.jar; 赠送原API文档:cxf-rt-transports-http-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-transports-http-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-transports-...

    cxf-rt-rs-client-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-rs-client-3.0.1.jar; 赠送原API文档:cxf-rt-rs-client-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-rs-client-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-rs-client-3.0.1.pom; 包含...

    cxf-rt-frontend-simple-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-frontend-simple-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-simple-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-simple-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-...

    webservice的cxf框架客户端调用所需jar包

    1. `cxf-core-3.2.4.jar`: 这是CXF的核心模块,包含了处理SOAP消息、WSDL定义解析、服务实例化等核心功能。它负责构建并管理Web服务的生命周期,同时也是其他CXF模块的基础。 2. `xmlbeans-2.6.0.jar`: XMLBeans是...

    cxf-rt-frontend-jaxws-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-frontend-jaxws-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-jaxws-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-jaxws-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-jaxws...

    CXF客户端支持最少jar

    2. **cxf-rt-frontend-jaxws.jar**:用于处理JAX-WS(Java API for XML Web Services)相关的功能,如WSDL(Web Service Description Language)处理、SOAP消息生成等。 3. **cxf-rt-transports-http.jar**:提供...

    cxf-rt-bindings-soap-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-bindings-soap-3.0.1.jar; 赠送原API文档:cxf-rt-bindings-soap-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-bindings-soap-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-bindings-soap-...

    cxf-rt-frontend-jaxrs-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-frontend-jaxrs-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-jaxrs-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-jaxrs-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-jaxrs...

    cxf框架包 apache-cxf-3.4.3.tar.gz

    在本案例中,我们讨论的是"apache-cxf-3.4.3.tar.gz",这是Apache CXF 3.4.3版本的压缩包,通常包含了CXF框架的所有组件和必要的库文件。 **1. CXF框架介绍** Apache CXF是一个全面的服务开发框架,它的全称是...

    cxfWebservice客户端全部jar包及极简调用方法.rar

    cxf-core-3.0.0.jar,cxf-rt-bindings-soap-3.0.0.jar,cxf-rt-databinding-jaxb-3.0.0.jar,cxf-rt-frontend-jaxws-3.0.0.jar,cxf-rt-frontend-simple-3.0.0.jar,cxf-rt-transports-http-3.0.0.jar,cxf-rt-...

    cxf-rt-rs-extension-providers-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-rs-extension-providers-3.0.1.jar; 赠送原API文档:cxf-rt-rs-extension-providers-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-rs-extension-providers-3.0.1-sources.jar; 赠送Maven依赖信息...

    cxf-rt-databinding-jaxb-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-databinding-jaxb-3.0.1.jar; 赠送原API文档:cxf-rt-databinding-jaxb-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-databinding-jaxb-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-...

    apache-cxf-3.5.0.zip

    "apache-cxf-3.5.0.zip" 文件包含了CXF框架的3.5.0版本,该版本可能包含了一些新特性、改进和错误修复。 1. **Apache CXF 框架概述** Apache CXF 是基于Java的,它允许开发者以Java编程语言来创建并暴露Web服务。...

    cxf-rt-frontend-jaxws-3.0.16.jar 下载

    cxf-rt-frontend-jaxws-3.0.16.jar jar包下载3.0.16版本下载

    cxf-rt-bindings-xml-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-bindings-xml-3.0.1.jar; 赠送原API文档:cxf-rt-bindings-xml-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-bindings-xml-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-bindings-xml-3.0.1....

    cxf-rt-ws-addr-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-ws-addr-3.0.1.jar; 赠送原API文档:cxf-rt-ws-addr-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-ws-addr-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-ws-addr-3.0.1.pom; 包含翻译后的API...

    cxf-core-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-core-3.0.1.jar; 赠送原API文档:cxf-core-3.0.1-javadoc.jar; 赠送源代码:cxf-core-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-core-3.0.1.pom; 包含翻译后的API文档:cxf-core-3.0.1-...

    cxf-rt-transports-http-jetty-3.0.2.zip

    标题 "cxf-rt-transports-http-jetty-3.0.2.zip" 指的是Apache CXF项目的一个特定版本的HTTP-Jetty传输模块。Apache CXF是一个流行的开源框架,用于构建和开发服务导向架构(SOA)和RESTful Web服务。这个版本,...

Global site tag (gtag.js) - Google Analytics