java - 如何更改 Google 应用引擎上 Web 服务调用的默认超时?

标签 java web-services google-app-engine webservice-client

我的 Google App Engine 应用程序使用 Web 服务,该 Web 服务响应速度非常慢,有时我的应用程序会崩溃:

java.net.SocketTimeoutException: Timeout while fetching URL: http://...

为了调用此 Web 服务,我使用 wsimport(用于解析现有 WSDL 文件并生成所需文件的 Java 工具)生成的类。

我需要更改此调用或全局所有应用 URL 提取的默认截止时间(5 秒)。

应用引擎文档:

You can set a deadline for a request, the most amount of time the service will wait for a response. By default, the deadline for a fetch is 5 seconds. The maximum deadline is 60 seconds for HTTP requests and 10 minutes for task queue and cron job requests. When using the URLConnection interface, the service uses the connection timeout (setConnectTimeout()) plus the read timeout (setReadTimeout()) as the deadline.

Source : https://developers.google.com/appengine/docs/java/urlfetch/#Java_Making_requests

我尝试在我的代码中添加此行(下面的强字)来更改截止日期,但它不起作用:

URL urlConnection = new URL(url);

URLConnection connection = urlConnection.openConnection();

connection.setConnectTimeout(180000); // 3 minutes

connection.setReadTimeout(180000); // 3 minutes

SWS webService = new SWS(urlConnection, new QName("http://...", "SWS"));

注意:SWS 是 wsimport 从我的 WSDL 生成的主类

最佳答案

一分钟前在此发布,因为也没有接受的答案:Can I globally set the timeout of HTTP connections?

对于具有 JAX-WS 的 App Engine,您必须设置请求上下文(今天使用 SDK 1.9.15 进行了测试)。对于普通机器,您不能超过 60 秒,并且必须切换到更大的机器 (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;
}

关于java - 如何更改 Google 应用引擎上 Web 服务调用的默认超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21484221/

相关文章:

iphone - 亚马逊网络服务问题。我应该为网络服务付费吗?

c# - 如何在 c# 中查看对服务引用的传入响应的完整 SOAP 响应(包括 header )?

python - 检索保存到 blobstore 的照片的元数据

python - Google 应用引擎解析 xml 超过 1 mb

java - 如何防止 webview 在方向改变时改变文本大小?

java - 如果 Java 版本为 1.5 或 1.6,则 ant 构建失败

web-services - "Web Services"GlassFish Server 开源版 3.0.1 中缺少节点

Java运行curl命令在Windows和Linux上是不同的

java - 仅针对某些 jar 将 System.out/System.err 重定向到 log4j

java - 在 App Engine 上将实体转换为它的类类型