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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在调用第三方提供的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)atorg.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:313)atorg.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:265)atorg.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73)atorg.apache.cxf.frontend.ClientProxy.invoke(ClientProxy.java:68)atcom.sun.proxy.Proxy32.bizECQuery(Unknown Source)
at com.sinosoft.test.Test.main(Test.java:400)
Caused by: java.net.SocketTimeoutException: SocketTimeoutException invoking http://localhost:8080/services/IBizECQuery?wsdl: Read timed out
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.apache.cxf.transport.http.HTTPConduit W r a p p e d O u t p u t S t r e a m . m a p E x c e p t i o n ( H T T P C o n d u i t . j a v a : 2058 ) a t o r g . a p a c h e . c x f . t r a n s p o r t . h t t p . H T T P C o n d u i t WrappedOutputStream.mapException(HTTPConduit.java:2058) at org.apache.cxf.transport.http.HTTPConduit WrappedOutputStream.mapException(HTTPConduit.java:2058)atorg.apache.cxf.transport.http.HTTPConduitWrappedOutputStream.close(HTTPConduit.java:2043)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:66)
at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:639)
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 : 62 ) . . . 8 m o r e C a u s e d b y : j a v a . n e t . S o c k e t T i m e o u t E x c e p t i o n : R e a d t i m e d o u t a t j a v a . n e t . S o c k e t I n p u t S t r e a m . s o c k e t R e a d 0 ( N a t i v e M e t h o d ) a t j a v a . n e t . S o c k e t I n p u t S t r e a m . s o c k e t R e a d ( S o c k e t I n p u t S t r e a m . j a v a : 116 ) a t j a v a . n e t . S o c k e t I n p u t S t r e a m . r e a d ( S o c k e t I n p u t S t r e a m . j a v a : 171 ) a t j a v a . n e t . S o c k e t I n p u t S t r e a m . r e a d ( S o c k e t I n p u t S t r e a m . j a v a : 141 ) a t j a v a . i o . B u f f e r e d I n p u t S t r e a m . f i l l ( B u f f e r e d I n p u t S t r e a m . j a v a : 246 ) a t j a v a . i o . B u f f e r e d I n p u t S t r e a m . r e a d 1 ( B u f f e r e d I n p u t S t r e a m . j a v a : 286 ) a t j a v a . i o . B u f f e r e d I n p u t S t r e a m . r e a d ( B u f f e r e d I n p u t S t r e a m . j a v a : 345 ) a t s u n . n e t . w w w . h t t p . H t t p C l i e n t . p a r s e H T T P H e a d e r ( H t t p C l i e n t . j a v a : 704 ) a t s u n . n e t . w w w . h t t p . H t t p C l i e n t . p a r s e H T T P ( H t t p C l i e n t . j a v a : 647 ) a t s u n . n e t . w w w . p r o t o c o l . h t t p . H t t p U R L C o n n e c t i o n . g e t I n p u t S t r e a m 0 ( H t t p U R L C o n n e c t i o n . j a v a : 1569 ) a t s u n . n e t . w w w . p r o t o c o l . h t t p . H t t p U R L C o n n e c t i o n . g e t I n p u t S t r e a m ( H t t p U R L C o n n e c t i o n . j a v a : 1474 ) a t j a v a . n e t . H t t p U R L C o n n e c t i o n . g e t R e s p o n s e C o d e ( H t t p U R L C o n n e c t i o n . j a v a : 480 ) a t o r g . a p a c h e . c x f . t r a n s p o r t . h t t p . H T T P C o n d u i t MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62) ... 8 more Caused by: java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:171) at java.net.SocketInputStream.read(SocketInputStream.java:141) at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) at java.io.BufferedInputStream.read1(BufferedInputStream.java:286) at java.io.BufferedInputStream.read(BufferedInputStream.java:345) at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:704) at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1569) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) at org.apache.cxf.transport.http.HTTPConduit MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)...8moreCausedby:java.net.SocketTimeoutException:Readtimedoutatjava.net.SocketInputStream.socketRead0(NativeMethod)atjava.net.SocketInputStream.socketRead(SocketInputStream.java:116)atjava.net.SocketInputStream.read(SocketInputStream.java:171)atjava.net.SocketInputStream.read(SocketInputStream.java:141)atjava.io.BufferedInputStream.fill(BufferedInputStream.java:246)atjava.io.BufferedInputStream.read1(BufferedInputStream.java:286)atjava.io.BufferedInputStream.read(BufferedInputStream.java:345)atsun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:704)atsun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)atsun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1569)atsun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)atjava.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)atorg.apache.cxf.transport.http.HTTPConduitWrappedOutputStream.handleResponseInternal(HTTPConduit.java:2165)
at org.apache.cxf.transport.http.HTTPConduit W r a p p e d O u t p u t S t r e a m . h a n d l e R e s p o n s e ( H T T P C o n d u i t . j a v a : 2134 ) a t o r g . a p a c h e . c x f . t r a n s p o r t . h t t p . H T T P C o n d u i t WrappedOutputStream.handleResponse(HTTPConduit.java:2134) at org.apache.cxf.transport.http.HTTPConduit WrappedOutputStream.handleResponse(HTTPConduit.java:2134)atorg.apache.cxf.transport.http.HTTPConduitWrappedOutputStream.close(HTTPConduit.java:1988)
… 11 more

处理超时的时候我们想捕获java.net.SocketTimeoutException异常是捕获不到的,但是有时又需要根据超时返回响应的结果。
经过认真思考终于发现了解决办法:
catch (Exception e) {
e.printStackTrace();
Throwable cause = e.getCause();
if (cause.getMessage().indexOf(“Connection refused: connect”) > 0){
System.out.println(“连接异常”);
}else if (cause.getMessage().indexOf(“Read timed out”) > 0){
System.out.println(“响应超时”);
}else if (cause.getMessage().indexOf(“connect timed out”) > 0){
System.out.println(“连接超时”);
}else{
System.out.println(“处理异常”);
}
}

连接异常:地址连接不可用,可能是服务端没有启动。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值