我正在使用 Metro 2.0 和 J2SE5。我编写的应用程序在编译时不知道外部 WebService,它在运行时根据业务逻辑 XML 文件找到它们,因此我执行 WSDL 请求。
我写的示例代码如下:
字符串 wsdlServiceName = ...;
字符串 wsdlURL = ...;
文档有效负载 = ...;
final 字符串 nsURI = ...;
final QName serviceName = new QName(nsURI, wsdlServiceName + "Service");
final QName servicePort = new QName(nsURI, wsdlServiceName + "端口");
//为 SOAP 消息创建服务和调度程序
服务 service = Service.create(new URL(wsdlURL), serviceName);
dispatch = service.createDispatch(servicePort, SOAPMessage.class, Service.Mode.MESSAGE);
//设置超时
dispatch.getRequestContext().put("com.sun.xml.internal.ws.request.timeout", 3000);
dispatch.getRequestContext().put("com.sun.xml.internal.ws.connect.timeout", 3000);
//创建传出 SOAP 请求
SOAPBinding soapBinding = (SOAPBinding) dispatch.getBinding();
request = soapBinding.getMessageFactory().createMessage();
SOAPBody requestBody = request.getSOAPBody();
requestBody.addDocument(负载);
//调用网络服务操作
SOAPMessage 响应 = dispatch.invoke(request);
调用 Web 服务 ( dispatcher.invoke(request) ) 时超时正常工作
但是在设置超时之前请求 WSDL,如果 Web 服务没有响应,连接超时前需要 90 秒。
是否可以在请求 WSDL 之前设置超时?您需要一个调度程序来设置超时,但这是在创建请求 WSDL 的服务之后完成的?! (即 Service.create() )
最佳答案
尝试设置系统属性
sun.net.client.defaultConnectTimeout
但来自Networking Properties它说在未来的版本中可能不支持它
但是我建议缓存 WSDL 而不是远程访问它。
这是更好的性能明智,特别是如果您正在使用预计不会经常更改的 WSDL。
关于java - JAXWS - 设置 WSDL 请求超时所需的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4387393/