2012-03-29 77 views
16

我有一個程序使用javax.xml.ws.Service來調用由WSDL定義的遠程服務。該程序在Google App Engine上運行,默認情況下,該程序會將HTTP連接超時設置爲5秒{1}。我需要增加此超時值,因爲此服務通常需要很長時間才能響應,但由於此請求不是通過URLConnection進行的,因此我無法弄清楚如何調用URLConnection.setReadTimeout(int) {2},或者更改超時值。我可以全局設置HTTP連接的超時時間嗎?

有沒有什麼辦法在App Engine上全局設置HTTP連接超時?而且,爲了分享知識,人們通常會如何解決這類問題?

{1}:https://developers.google.com/appengine/docs/java/urlfetch/overview#Requests

{2}:http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLConnection.html#setReadTimeout(int)

回答

4

https://developers.google.com/appengine/docs/java/urlfetch/usingjavanet

你可以做這樣的事情得到一個URLConnection:

URL url = new URL("http://www.example.com/atom.xml"); 
    URLConnection tempConnection = url.openConnection(); 
    tempConnection.setReadTimeout(10); 
+0

整個問題是'URLConnection'對象永遠不可用。請求是以不透明的方式使用'javax.xml.ws.Service'進行的 – 2012-05-05 00:11:39

8

試試這個:

Port port = service.getPort(endPointInterface); //or another "getPort(...)" 
((BindingProvider) port).getRequestContext() 
    .put(BindingProviderProperties.REQUEST_TIMEOUT, 30); 
+0

我還沒有嘗試過,但是您得到了提供我還沒有在其他任何地方看到的答案的賞金。 – 2012-05-23 15:04:41

12

您可以嘗試設置sun.net.client.defaultConnectTimeoutsun.net.client.defaultReadTimeout系統屬性記錄爲here,例如,

System.setProperty("sun.net.client.defaultReadTimeout", "30000"); 
System.setProperty("sun.net.client.defaultConnectTimeout", "30000"); 

編輯

對不起,只是重新閱讀,並注意到這是在谷歌應用程序引擎。我不知道,但鑑於谷歌和甲骨文最近的訴訟關係,我猜測GAE不會運行Oracle JVM。如果有人遇到類似的問題,我會在這裏留下。

+0

鏈接到文檔:http://docs.oracle.com/javase/6/docs/technotes/guides/net/properties.html – 2013-04-29 22:00:02

1

對於帶有JAX-WS的App Engine,您必須設置請求上下文(現在使用SDK 1.9.15進行測試)。對於普通機器,你不能超過60s,爲了更好地使用任務隊列,必須切換到更大的機器(Bx)。

對於本地測試,您通常會使用BindingProviderProperties.CONNECT_TIMEOUT和BindingProviderProperties.REQUEST_TIMEOUT,但它們不在App Engine JRE白名單中,您的代碼檢查可能會不斷提醒您。 等效字符串可以使用,雖然:

com.sun.xml.internal.ws.connect.timeout 
com.sun.xml.internal.ws.connect.timeout 

對於部署到App Engine:

com.sun.xml.ws.connect.timeout 
com.sun.xml.ws.request.timeout 

完整的例子如何應用到自動生成的代碼從JAX-WS 2.x中,值必須以毫秒爲單位提供:

@WebEndpoint(name = "Your.RandomServicePort") 
public YourServiceInterface getYourRandomServicePort() { 
    YourRandomServiceInterface port = super.getPort(YOURRANDOMSERVICE_QNAME_PORT, YourRandomServiceInterface.class); 
    Map<String, Object> requestContext = ((BindingProvider)port).getRequestContext(); 
    requestContext.put("com.sun.xml.ws.connect.timeout", 10000); 
    requestContext.put("com.sun.xml.ws.request.timeout", 10000); 
    return port; 
} 
相關問題