Ale*_*lex 5 java soap java-metro-framework jax-ws
我正在使用Metro 2.0和J2SE5。我编写的应用程序在编译时不了解外部WebService,它在运行时基于业务逻辑XML文件找到它们,因此我执行WSDL请求。
我编写的示例代码如下:
String wsdlServiceName = ...;
String wsdlURL = ...;
Document payload = ...;
final String nsURI = ...;
final QName serviceName = new QName(nsURI, wsdlServiceName + "Service");
final QName servicePort = new QName(nsURI, wsdlServiceName + "Port");
// Create service and the dispatcher for the SOAP message
Service service = Service.create(new URL(wsdlURL), serviceName);
dispatch = service.createDispatch(servicePort, SOAPMessage.class, Service.Mode.MESSAGE);
// Set timeouts
dispatch.getRequestContext().put("com.sun.xml.internal.ws.request.timeout", 3000);
dispatch.getRequestContext().put("com.sun.xml.internal.ws.connect.timeout", 3000);
// Create the outgoing SOAP request
SOAPBinding soapBinding = (SOAPBinding) dispatch.getBinding();
request = soapBinding.getMessageFactory().createMessage();
SOAPBody requestBody = request.getSOAPBody();
requestBody.addDocument(payload);
// Invoke web service operation
SOAPMessage response = dispatch.invoke(request);
Run Code Online (Sandbox Code Playgroud)
调用Web服务时,超时正确工作(dispatcher.invoke(request))
但是,在设置超时之前需要WSDL,并且如果Web Service没有响应,则连接超时需要90秒。
是否可以在请求WSDL之前设置超时?您需要一个调度程序来设置超时时间,但这是在创建请求WSDL的服务之后完成的!(即Service.create())
尝试设置系统属性
sun.net.client.defaultConnectTimeout
Run Code Online (Sandbox Code Playgroud)
但从Networking Properties来看,它表示未来版本可能不支持它
不过,我建议缓存 WSDL,而不是远程访问它。
它的性能更好,尤其是当您使用预计不会频繁更改的 WSDL 时。