使用CXF调用webservice接口是超时异常的捕获处理

在使用CXF调用第三方WebService接口时,可以设置HTTPClientPolicy的连接和接收超时时间。当出现SocketTimeoutException时,直接捕获异常无法区分是连接超时还是响应超时。解决方法是通过分析异常的cause.getMessage(),根据其中的错误信息判断超时类型,从而给出相应的响应处理。例如,检查异常信息中是否包含'Connection refused: connect'、'Read timed out'和'connect timed out'来分别处理连接、响应和连接超时情况。
摘要由CSDN通过智能技术生成

在调用第三方提供的webservice接口时我们可以设置超时时间:
HTTPClientPolicy client = new HTTPClientPolicy();
client.setReceiveTimeout(1000 * 10); // 该时间为响应超时。
client.setConnectionTimeout(1000 * 20 ); // 连接超时。
超时后异常:
请求超时 java.net.SocketTimeoutException: connect timed out
响应超时 java.net.SocketTimeoutException: Read timed out
org.apache.cxf.interceptor.Fault: Could not send Message.
at org.apache.cxf.interceptor.MessageSenderInterceptor M e s s a g e S e n d e r E n d i n g I n t e r c e p t o r . h a n d l e M e s s a g e ( M e s s a g e S e n d e r I n t e r c e p t o r . j a v a : 64 ) a t o r g . a p a c h e . c x f . p h a s e . P h a s e I n t e r c e p t o r C h a i n . d o I n t e r c e p t ( P h a s e I n t e r c e p t o r C h a i n . j a v a : 243 ) a t o r g . a p a c h e . c x f . e n d p o i n t . C l i e n t I m p l . i n v o k e ( C l i e n t I m p l . j a v a : 487 ) a t o r g . a p a c h e . c x f . e n d p o i n t . C l i e n t I m p l . i n v o k e ( C l i e n t I m p l . j a v a : 313 ) a t o r g . a p a c h e . c x f . e n d p o i n t . C l i e n t I m p l . i n v o k e ( C l i e n t I m p l . j a v a : 265 ) a t o r g . a p a c h e . c x f . f r o n t e n d . C l i e n t P r o x y . i n v o k e S y n c ( C l i e n t P r o x y . j a v a : 73 ) a t o r g . a p a c h e . c x f . f r o n t e n d . C l i e n t P r o x y . i n v o k e ( C l i e n t P r o x y . j a v a : 68 ) a t c o m . s u n . p r o x y . MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64) at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243) at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:487) at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:313) at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:265) at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73) at org.apache.cxf.frontend.ClientProxy.invoke(ClientProxy.java:68) at com.sun.proxy. MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)atorg.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243)atorg.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:487

Apache CXF是一个开源的WebService框架,可以帮助用户快速、简便地开发和部署WebService应用程序。它提供了多种方式来调用WebService接口,下面介绍几种常用的方式: 1. 使用JAX-WS API:CXF实现了JAX-WS API,可以直接使用JAX-WS提供的API来调用WebService。示例代码如下: ```java HelloWorldService service = new HelloWorldService(); HelloWorld port = service.getHelloWorldPort(); String result = port.sayHello("CXF"); ``` 2. 使用代理方式:CXF可以根据WebService WSDL文件自动生成代理类,然后通过调用代理类的方法来调用WebService接口。示例代码如下: ```java JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(HelloWorld.class); factory.setAddress("http://localhost:8080/HelloWorld"); HelloWorld client = (HelloWorld) factory.create(); String result = client.sayHello("CXF"); ``` 3. 使用Spring配置文件:CXF提供了Spring配置文件方式来实现WebService接口调用。用户可以在Spring配置文件中配置WebService客户端,然后通过Spring容器来获取WebService客户端实例。示例代码如下: ```xml <jaxws:client id="helloClient" serviceClass="com.example.HelloWorld" address="http://localhost:8080/HelloWorld"/> ``` ```java ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); HelloWorld client = (HelloWorld) context.getBean("helloClient"); String result = client.sayHello("CXF"); ``` 以上是几种常用的调用WebService接口的方式,可以根据具体情况选择适合自己的方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值