使用Metro 2.0和J2SE5的Im。我編寫的應用程序在編譯時並不知道外部WebService,它在運行時基於業務邏輯XML文件找到它們,因此我執行WSDL請求。JAXWS - 設置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);
超時工作正常時,Web服務被調用(dispatcher.invoke(請求))
然而WSDL是在超時設置之前請求,並且如果Web服務沒有響應,則連接超時前需要90秒。
是否可以在請求WSDL之前設置超時?您需要一個調度程序來設置超時,但這是在創建請求WSDL的服務之後完成的?! (即Service.create())
嗨。感謝您的評論,我會嘗試一下。出於興趣,你知道我的程序是否可以修改,以在請求WSDL之前設置ws-timeout?我會認爲這應該是可能的? – Alex 2010-12-08 13:56:35
@Alex:我認爲WSDL可以通過`Service.create(new URL(wsdlURL),serviceName);`進行訪問。由於超時設置在調度程序上,因此我無法看到如何設置它。雖然系統屬性應該可以幫助你,因爲jax-ws使用HttpUrlConnection封面來與服務器進行通信。恕我直言,雖然你應該保持wsdl緩存,以避免遠程獲取,也避免了這些問題。 – Cratylus 2010-12-08 16:43:48