我可以全局设置HTTP连接的超时时间吗?
我有一个程序使用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)
见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);
试试这个:
Port port = service.getPort(endPointInterface); //or another "getPort(...)"
((BindingProvider) port).getRequestContext()
.put(BindingProviderProperties.REQUEST_TIMEOUT, 30);
我还没有尝试过,但是您得到了提供我还没有在其他任何地方看到的答案的赏金。 – 2012-05-23 15:04:41
您可以尝试设置sun.net.client.defaultConnectTimeout
和sun.net.client.defaultReadTimeout
系统属性记录为here,例如,
System.setProperty("sun.net.client.defaultReadTimeout", "30000");
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
编辑
对不起,只是重新阅读,并注意到这是在谷歌应用程序引擎。我不知道,但鉴于谷歌和甲骨文最近的诉讼关系,我猜测GAE不会运行Oracle JVM。如果有人遇到类似的问题,我会在这里留下。
链接到文档:http://docs.oracle.com/javase/6/docs/technotes/guides/net/properties.html – 2013-04-29 22:00:02
对于带有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;
}
整个问题是'URLConnection'对象永远不可用。请求是以不透明的方式使用'javax.xml.ws.Service'进行的 – 2012-05-05 00:11:39