2010-12-08 193 views
3

使用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())

回答

1

嘗試設置系統屬性

sun.net.client.defaultConnectTimeout 

Networking Properties它說,它可能不會在將來的版本支持的

不過,我會建議緩存WSDL並不能遠程訪問它。
它更好的性能特別是如果您正在使用不期望經常更改的WSDL。

+0

嗨。感謝您的評論,我會嘗試一下。出於興趣,你知道我的程序是否可以修改,以在請求WSDL之前設置ws-timeout?我會認爲這應該是可能的? – Alex 2010-12-08 13:56:35

+0

@Alex:我認爲WSDL可以通過`Service.create(new URL(wsdlURL),serviceName);`進行訪問。由於超時設置在調度程序上,因此我無法看到如何設置它。雖然系統屬性應該可以幫助你,因爲jax-ws使用HttpUrlConnection封面來與服務器進行通信。恕我直言,雖然你應該保持wsdl緩存,以避免遠程獲取,也避免了這些問題。 – Cratylus 2010-12-08 16:43:48

1

我們剛剛遇到了同樣的問題,並嘗試了上面提到的所有設置 - 同樣,也無濟於事。

我們的解決方案是首先使用URL.openConnection()(使用URLConnection.setConnectionTimeout()和URLConnection.setReadTimeout())在連接上設置超時來將WSDL下載到臨時文件。然後我們用這個文件生成一個url:File.toURI()。toURL(),我們把它傳遞給帶有URL的服務構造函數。

此方法可讓您動態獲取當前WSDL,同時明確控制超時。然後,我們爲您在原始帖子中顯示的服務設置超時時間。