CXF发送、接收消息超时设置

在使用WebService时,我们通常都会在客户端中设置请求超时的限制,以避免长时间的去连接不可用的服务器。在CXF的环境下,客户端可通过两个属性配置超时限制:

  • ConnectionTimeout - WebService以TCP连接为基础,这个属性可以理解为TCP握手时的时间设置,超过设置的时间就认为是连接超时.以毫秒为单位,默认是30000毫秒,即30秒。 
  • ReceiveTimeout - 这个属性是发送WebService的请求后等待响应的时间,超过设置的时长就认为是响应超时.以毫秒为单位,默认是60000毫秒,即60秒.
Java代码   收藏代码
  1. Client client = ClientProxy.getClient(port);    
  2. HTTPConduit http = (HTTPConduit) client.getConduit();    
  3. HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();    
  4. httpClientPolicy.setConnectionTimeout(36000);    
  5. httpClientPolicy.setAllowChunking(false);    
  6. httpClientPolicy.setReceiveTimeout(32000);    
  7. http.setClient(httpClientPolicy);   

 spring+cxf配置方式:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>      
  2.    <beans xmlns="http://www.springframework.org/schema/beans"      
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
  4.         xmlns:jee="http://www.springframework.org/schema/jee"      
  5.         xmlns:jaxws="http://cxf.apache.org/jaxws"      
  6.         xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"       
  7.         xsi:schemaLocation="http://www.springframework.org/schema/beans     
  8.              http://www.springframework.org/schema/beans/spring-beans-2.0.xsd      
  9.              http://www.springframework.org/schema/jee     
  10.              http://www.springframework.org/schema/jee/spring-jee-2.0.xsd      
  11.              http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd      
  12.              http://cxf.apache.org/transports/http/configuration     
  13.              http://cxf.apache.org/schemas/configuration/http-conf.xsd ">      
  14.        <http-conf:conduit name="{WSDL Namespace}portName.http-conduit">       
  15.           <http-conf:client ConnectionTimeout="10000" ReceiveTimeout="20000"/>      
  16.        </http-conf:conduit>       
  17.    </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属性,指定设置生效的服务。name属性由service的namespace、WSDL中的 port name和".http-conduit"组成,如{http://apache.org/hello_world}HelloWorld.http- conduit。如果将name属性设置为“*.http-conduit”,则会对所有服务生效。

     另外,WSDL中的endpoint的地址不一定是有效的,为避免客户端请求使用该地址,我们在请求前应通过以下方式强行设置为可用的服务地址。
  ((BindingProvider) port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,serviceUrl);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用CXF的UsernameToken来设置用户名和密码。下面是一个示例: 首先,您需要在您的CXF配置文件中启用WS-Security功能。在Spring配置文件中添加以下内容: ```xml <jaxws:endpoint id="yourServiceEndpoint" implementor="com.yourpackage.YourServiceImpl"> <jaxws:features> <bean class="org.apache.cxf.feature.LoggingFeature"/> <bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor"> <constructor-arg> <map> <entry key="action" value="UsernameToken"/> <entry key="passwordType" value="PasswordText"/> <entry key="passwordCallbackClass" value="com.yourpackage.PasswordCallbackHandler"/> </map> </constructor-arg> </bean> </jaxws:features> </jaxws:endpoint> ``` 在这个示例中,我们启用了LoggingFeature和WSS4JInInterceptor。WSS4JInInterceptor负责处理WS-Security的UsernameToken,它使用PasswordCallbackHandler来验证用户名和密码。 接下来,您需要实现一个PasswordCallbackHandler类来验证提供的用户名和密码。例如: ```java public class PasswordCallbackHandler implements CallbackHandler { @Override public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (Callback callback : callbacks) { WSPasswordCallback pc = (WSPasswordCallback) callback; // 在这里验证用户名和密码 if ("your_username".equals(pc.getIdentifier()) && "your_password".equals(pc.getPassword())) { return; } else { throw new SecurityException("Invalid username or password"); } } } } ``` 在这个示例中,我们简单地比较提供的用户名和密码与预先定义的值。您可以根据自己的需求来实现更复杂的验证逻辑。 最后,您可以使用CXF创建客户端来调用带有用户名和密码的服务。例如: ```java YourService service = new YourService(); YourServicePortType port = service.getPort(YourServicePortType.class); BindingProvider bp = (BindingProvider) port; bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "your_username"); bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "your_password"); // 调用服务方法 port.yourServiceMethod(); ``` 在这个示例中,我们将用户名和密码通过BindingProvider设置到请求上下文中,从而将其传递给服务端。 请注意替换示例代码中的"your_username"和"your_password"为实际的用户名和密码。 希望这可以帮助到您!如果您有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值