JAXWS - 设置WSDL请求超时所需的帮助

时间:2010-12-08 12:26:50

标签: java soap jax-ws java-metro-framework

我正在使用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);

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);

调用Web Service时,超时工作正常(dispatcher.invoke(request))

如果在设置超时之前请求了WSDL,并且如果Web服务没有响应,则在连接超时之前需要90秒。

是否可以在请求WSDL之前设置超时?您需要一个调度程序来设置超时,但这是在创建请求WSDL的服务之后完成的吗?! (即Service.create())

2 个答案:

答案 0 :(得分:2)

尝试设置系统属性

sun.net.client.defaultConnectTimeout 

但是来自Networking Properties它表示在将来的版本中可能不支持

但是我建议缓存WSDL而不是远程访问它 如果您正在使用预期不会经常更改的WSDL,那么性能会更好。

答案 1 :(得分:2)

我们遇到了同样的问题,并尝试了上面提到的所有设置 - 同样,无济于事。

我们的解决方案是首先使用URL.openConnection()将WSDL下载到临时文件(在连接上设置超时:URLConnection.setConnectionTimeout()和URLConnection.setReadTimeout())。然后,我们使用以下命令生成此文件的URL:File.toURI()。toURL(),我们将其传递给采用URL的服务构造函数。

此方法允许您动态获取当前WSDL,同时显式控制超时。然后,我们为您在原始帖子中显示的服务的后续调用设置超时。

相关问题