Spring下设置CXF的WebService客户端超时时长 配置

网上关于这个xml的配置代码都是一个版本。
自己经过配置测试都不起作用, 最后按照第2部分增加cxf.xml,单独对cxf进行配置,放到tomcat的class文件夹下,配置生效。


1.
Html代码   收藏代码
  1. 背景:  
  2. 在使用WebService的时候,我们可能需要一个备份的WebService服务器.一旦主服务器down了,我们可以使用备份的服务器.那么这里就需要对客服端连接服务器的时间做一个修改.  
  3.   
  4. 在Spring+CXF的WebService环境下,客户端有两个时间属性是可配置的,分别是ConnectionTimeout和ReceiveTimeout.  
  5. ConnectionTimeout--WebService以TCP连接为基础,这个属性可以理解为tcp的握手时的时间设置,超过设置的时间长则认为是连接超时.以毫秒为单位,默认是30000毫秒,即30秒.  
  6.   
  7. ReceiveTimeout -- 这个属性是发送WebService的请求后等待响应的时间,超过设置的时长就认为是响应超时.以毫秒为单位,默认是60000毫秒,即60秒.  
  8.   
  9. beans.xml 设置的例子:  
  10. <?xml version="1.0" encoding="UTF-8"?>  
  11. <beans xmlns="http://www.springframework.org/schema/beans"  
  12.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  13.     xmlns:jee="http://www.springframework.org/schema/jee"  
  14.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  15.     xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"   
  16.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  17.            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd  
  18.            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd  
  19.            http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd ">  
  20.         <http-conf:conduit name="{http://impl.service.product.super.com/}ProjectService.http-conduit">   
  21.         <http-conf:client ConnectionTimeout="10000" ReceiveTimeout="20000"/>  
  22.     </http-conf:conduit>   
  23. </beans>  
  24.   
  25.   
  26.   
  27. 这里需要注意的有几个地方:  
  28. 1:需要指定http-conf名称空间 xmlns:http-conf=http://cxf.apache.org/transports/http/configuration  
  29. 2:指定模式位置: http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd  
  30. 3:http-conf:conduit中的name属性,指定设置生效的服务,如例子中,只对服务名为{http://impl.service.product.sww.com/}ProjectService的服务生效.  
  31. 使用下面的设置则会对所有服务生效  
  32. <http-conf:conduit name="*.http-conduit">  
  33.         
  34. </http-conf:conduit>  
  35.   
  36.   
  37. 更详细的配置请参考CXF官方文档:  
  38.   
  39. http://cwiki.apache.org/CXF20DOC/client-http-transport-including-ssl-support.html  

2.

cxf

默认连接时间超时为30秒,等待响应时间为60秒。这就代表server挂了,会一直占用当前client的线程最长30秒以上。
建议:
3.1.cxf spring xml beans头文件增加:

2        xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
5 <http-conf:conduit name="*.http-conduit">
6    <http-conf:client ConnectionTimeout="2000" ReceiveTimeout="5000"/>单位为毫秒
7 </http-conf:conduit>

3.2.在资源文件目录下增加cxf.xml,cxf会自动读取(推荐):

02        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
03        xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
04        xsi:schemaLocation="http://cxf.apache.org/transports/http/configuration
05  
06 http://cxf.apache.org/schemas/configuration/http-conf.xsd
07  
08  
09 http://www.springframework.org/schema/beans
10  
11  
12 http://www.springframework.org/schema/beans/spring-beans.xsd">
13  
14       <http-conf:conduit name="*.http-conduit">
15         <http-conf:client ConnectionTimeout="2000" ReceiveTimeout="5000"/>单位为毫秒
16        </http-conf:conduit>
17 </beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值