在调用第三方提供的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.MessageSenderInterceptorMessageSenderEndingInterceptor.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.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.HTTPConduitWrappedOutputStream.mapException(HTTPConduit.java:2058)atorg.apache.cxf.transport.http.HTTPConduitWrappedOutputStream.mapException(HTTPConduit.java:2058) at org.apache.cxf.transport.http.HTTPConduitWrappedOutputStream.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.MessageSenderInterceptorMessageSenderEndingInterceptor.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.HTTPConduitMessageSenderEndingInterceptor.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.HTTPConduitMessageSenderEndingInterceptor.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.HTTPConduitWrappedOutputStream.handleResponse(HTTPConduit.java:2134)atorg.apache.cxf.transport.http.HTTPConduitWrappedOutputStream.handleResponse(HTTPConduit.java:2134) at org.apache.cxf.transport.http.HTTPConduitWrappedOutputStream.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(“处理异常”);
}
}

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

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

  1. spring+cxf调用webservice接口

    一.导入Spring和cxf的jar,在第一节中有说明. 二.编写Spring配置文件ClientBeans.xml ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  2. 使用cxf发布webservice接口,以及调用webservice接口

    一.cxf发布webservice接口 添加maven依赖 <dependency><groupId>org.apache.cxf</groupId><art ...

  3. Apache CXF生成java代码用java调用Webservice接口

    这里写自定义目录标题 使用java调用webservice接口 使用java调用webservice接口 当我们使用java去访问webservice的接口时,有很多种方式,这里我们使用apache ...

  4. 调用webservice接口,数据不回滚问题

    调用webservice接口,数据不回滚问题 使用cxf+spring框架开发webservice接口,在开发一个具有保存数据的接口功能时,如果数据发生了异常,对service层无论使用了xml配置声 ...

  5. apache cxf 测试webservice接口

    2019独角兽企业重金招聘Python工程师标准>>> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...

  6. 调用webservice接口,报错:(十六进制值0x01)是无效的字符。

    文章目录 #事故现场 #分析 #解决方法 #事故现场 调用webservice接口,报错:(十六进制值0x01)是无效的字符. 如图: 意思是webservice返回的信息中包含无效的字符,无法解析成 ...

  7. java调用接口速度慢,Java 调用 webservice 接口,为什么这么慢

    Java 调用 webservice 接口,为什么这么慢,有没有好的优化方案,本人目前改成 http 方式发 soap 消息调用,速度依旧很慢,维持在平均 100ms 下不来. 之前用 cxf 的 J ...

  8. java 调用webservice 接口,并解析返回参数

    java 调用webservice 接口,并解析返回参数 1. 设置传参,例如以下格式: // 确定传参格式以及赋值 String reqXml = "<createAppParam& ...

  9. python suds_python使用suds调用webservice接口的方法

    最近做接口对接,遇到了.net开发的webservice接口,因为python第一次与webservice对接,连问带查,最后使用suds库来实现了 1.安装suds mac: sudo pip in ...

最新文章

  1. 【AngularJS】—— 9 自定义过滤器
  2. 安装php出现php-cgi error 1
  3. C#中String与Datetime
  4. router3 BGP1 基础部分
  5. C# 杀掉指定进程
  6. Prepared statement needs to be re-prepare
  7. 日志分析里面的max是什么_压测nginx出现no live upstreams while connecting to upstream的问题分析...
  8. ug区域轮廓铣没有重叠距离_UG加工基本操作
  9. Python模块的导入
  10. 油猴脚本(实习生趁手的工具)
  11. 数学建模 之 matlab初学两天搞定基础 极限求导积分篇(5/5)
  12. 【公司邮箱如何申请】怎么写加密邮件,企业邮箱支持吗?
  13. 前端vue项目下载zip压缩包及附加
  14. 中国大学mooc中JAVA的答案_中国大学mooc2020Java程序设计题目答案
  15. HiveSQL分位数函数percentile()使用详解+实例代码
  16. java读取xslx内容,内容转换成docx和pdf,包括图片
  17. to B和to C产品的区别
  18. Python的打包神器—Nuitka
  19. android adb调试技巧
  20. 语音对讲软件_微信语音转播软件哪个好?你有没有推荐的呢?

热门文章

  1. pipeline+sonar
  2. 字符串中size()、length()与sizeof()用法及区别
  3. 系统设计面试题 之 如何设计Pastebin.com
  4. 海贼王最大剧透!神预判,技术革新!
  5. 论文阅读及神经网络学习----Neural Networks for Multi-Instance Learning
  6. 元学习—高斯原型网络实现(Pytorch)
  7. 调用高德地图API将地名转换成经纬度
  8. 上海市企业信息化促进中心常务副主任罗钢:大数据分析的风险与安全
  9. 男女间互送礼物的含义大全.别送错…
  10. 大专生找工作,难啊!