在调用第三方提供的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
使用CXF调用webservice接口是超时异常的捕获处理
最新推荐文章于 2020-07-03 20:29:49 发布
在使用CXF调用第三方WebService接口时,可以设置HTTPClientPolicy的连接和接收超时时间。当出现SocketTimeoutException时,直接捕获异常无法区分是连接超时还是响应超时。解决方法是通过分析异常的cause.getMessage(),根据其中的错误信息判断超时类型,从而给出相应的响应处理。例如,检查异常信息中是否包含'Connection refused: connect'、'Read timed out'和'connect timed out'来分别处理连接、响应和连接超时情况。
摘要由CSDN通过智能技术生成