spring 整合WebService 设置超时

前段时间在用spring 整合WebService 设置超时时有个问题一直解决不了

最后不在spring配置文件里写直接获取代理类,对代理类进行设置。

例子:

package test;


import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.cxf.server.HolloWrold;


public class TestMessage {
public static void main(String[] args) {
ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
HolloWrold a =(HolloWrold) factory.getBean("holloWrold");
Client proxy = ClientProxy.getClient(a);
HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(3000); // 连接超时时间 30
policy.setReceiveTimeout(2400);// 请求超时时间. 4分钟240秒
//policy.setContentType("text/xml;charset=UTF-8");
//policy.setElementType(type);
conduit.setClient(policy);
System.out.println(a.sayHi("aaa"));
}
}




事后经过百度得知错误原因:

name 的不是wsdl上面的name (不知道什么原因,官方文档说的就是 等于 WSDL中的 port name和".http-conduit"组成

而是客户端接口的targetNamespace和name + Prot    

name="{targetNamespace }name+Port.http-conduit"

例如我客户端接口:  @WebService(targetNamespace = "http://server.cxf.com/", name = "HolloWrold")

name="{http://server.cxf.com/}HolloWroldPort.http-conduit"


<http-conf:conduit name="{http://server.cxf.com/}HolloWroldPort.http-conduit">   

          <http-conf:client ConnectionTimeout="3000" ReceiveTimeout="3000"  ContentType="text/xml;charset=UTF-8"/>  
</http-conf:conduit> 


例子:

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

<?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://server.cxf.com/}HolloWroldPort.http-conduit">   
          <http-conf:client ConnectionTimeout="3000" ReceiveTimeout="3000"  ContentType="text/xml;charset=UTF-8"/>  
    </http-conf:conduit> 
    
</beans>    

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


还有一种方法:

就是name = " WebService 路径"  只对这个 WebService 起作用(ContentType是设置编码格式)

例子:

<http-conf:conduit name="http://localhost:8080/WSSSpring/webservice/hollo">   
          <http-conf:client ConnectionTimeout="3000" ReceiveTimeout="3000"  ContentType="text/xml;charset=UTF-8"/>  
    </http-conf:conduit>  




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设置WebService调用超时时间,可以按照以下步骤进行操作。 首先,需要在WebService的客户端代理程序中设置请求超时时间。可以在GetWebRequest方法中设置超时时间,单位是毫秒。例如,可以将超时时间设置为30秒(30 * 1000毫秒)\[2\]。 其次,需要在调用WebService接口的代码中设置超时时间。可以在调用方法之前设置超时时间。例如,可以使用try-catch块来捕获异常,并在catch块中设置超时时间。可以使用setTimeOut方法来设置超时时间,单位是毫秒。例如,可以将超时时间设置为30秒(30 * 1000毫秒)\[3\]。 通过以上步骤,就可以设置WebService调用超时时间,以确保在超时时间内获取到响应。 #### 引用[.reference_title] - *1* *2* [调用webservice 设置超时时间](https://blog.csdn.net/qq_44884269/article/details/89321377)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Xfire生成WebService客户端代码 调用设置超时时间](https://blog.csdn.net/wzq0926/article/details/125758476)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值