CXF 连接超时与请求超时的设置

来源:互联网 发布:网络中介服务提供商 编辑:程序博客网 时间:2024/05/21 22:57
主要在客户端调用时设置 :
        代码在编译、运行是没问题的,但测试发现如果服务端没有启动或网络环境差,CXF会默认等待一定的时间~~~
        则有了客户端调用超时的设置;

方式 1 - 在代码中设置超时:

//这是命令生成的类,该类的实例可当成工厂来使用CXFEtcvRemindInfoServiceImpl factory = new CXFEtcvRemindInfoServiceImpl();//无参的方法,返回的是远程Web Service服务端的代理,服务端不能关闭。CXFEtcvRemindInfoService cxfPort = factory.getCXFEtcvRemindInfoServiceImplPort();Client client = ClientProxy.getClient(cxfPort);client.getOutInterceptors().add(new LoggingInInterceptor());//输出In信息的日志client.getOutInterceptors().add(new LoggingOutInterceptor());//输出Out信息的日志//传参的List<Bean>List<CxfEtcvRemindInfoPushInputParam> inputParams = new ArrayList<CxfEtcvRemindInfoPushInputParam>();CxfEtcvRemindInfoPushInputParam inputParam = new CxfEtcvRemindInfoPushInputParam();inputParam.setDepartureDate("2016-12-09");inputParams.add(inputParam);// 连接参数设定Client proxy = ClientProxy.getClient(cxfPort);HTTPConduit conduit = (HTTPConduit) proxy.getConduit();HTTPClientPolicy policy = new HTTPClientPolicy();// 连接超时时间policy.setConnectionTimeout(10000);// 请求超时时间policy.setReceiveTimeout(10000);conduit.setClient(policy);//调用接口,返回信息List<CxfEtcvRemindInfoPushOutputParam> outputParams = cxfPort.pushEVRemindInfo(inputParams);
超时设置核心代码为

         // 连接参数设定
         Client proxy = ClientProxy.getClient(cxfPort);
         HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
         HTTPClientPolicy policy = new HTTPClientPolicy();
         // 连接超时时间
         policy.setConnectionTimeout(10000);
         // 请求超时时间
         policy.setReceiveTimeout(10000);
         conduit.setClient(policy);

方式 2 - 在配置文件中设置超时(百度得到):

<?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="10000"/>          </http-conf:conduit>    </beans>

这里需要注意的有几个地方:   
         1:需要指定http-conf名称空间 xmlns:http-conf=http://cxf.apache.org/transports/http/configuration   
         2:指定模式位置: http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd   
         3:http-conf:conduit中的name属性,指定设置生效的服务,如例子中,只对服务名为{http://impl.service.product.sww.com/}ProjectService的服务生效.   
使用下面的设置则会对所有服务生效   
         <http-conf:conduit name="*.http-conduit">  
                  <http-conf:client……>
         </http-conf:conduit>

希望对你有帮助,祝你有一个好心情,加油!

若有错误、不全、可优化的点,欢迎纠正与补充;转载请注明出处!

1 0
原创粉丝点击