简体   繁体   English

如何更改Google App Engine上的Web服务调用的默认超时?

[英]How I can change default timeout for web services call on Google app engine?

My Google App Engine application use a web service, this web service is pretty slow to respond and sometimes my application crashes : 我的Google App Engine应用程序使用Web服务,该Web服务响应速度很慢,有时我的应用程序崩溃:

java.net.SocketTimeoutException: Timeout while fetching URL: http://... java.net.SocketTimeoutException:提取URL时超时:http:// ...

To call this web service, I use classes generated with wsimport (Java tool to parse an existing WSDL file and generate required files). 为了调用此Web服务,我使用wsimport生成的类(Java工具来解析现有的WSDL文件并生成所需的文件)。

I need to change the default deadline (5 seconds) either for this call or globally for all my app URL fetches. 我需要为此调用更改默认的截止期限(5秒),也可以为所有我的应用程序URL更改默认的截止期限。

App engine docs : App Engine文件:

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. 默认情况下,提取的截止日期为5秒。 The maximum deadline is 60 seconds for HTTP requests and 10 minutes for task queue and cron job requests. HTTP请求的最大期限为60秒,任务队列和cron作业请求的最大期限为10分钟。 When using the URLConnection interface, the service uses the connection timeout (setConnectTimeout()) plus the read timeout (setReadTimeout()) as the deadline. 使用URLConnection接口时,服务使用连接超时(setConnectTimeout())加上读取超时(setReadTimeout())作为截止日期。

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

I tried to add this lines (in strong below) in my code to change the deadline but it did'nt work : 我试图在我的代码中添加以下行(在下面的内容中)以更改截止日期,但是它没有用:

URL urlConnection = new URL(url); URL urlConnection =新的URL(URL);

URLConnection connection = urlConnection.openConnection(); URLConnection连接= urlConnection.openConnection();

connection.setConnectTimeout(180000); connection.setConnectTimeout(180000); // 3 minutes // 3分钟

connection.setReadTimeout(180000); connection.setReadTimeout(180000); // 3 minutes // 3分钟

SWS webService = new SWS(urlConnection, new QName("http://...", "SWS")); SWS webService =新的SWS(urlConnection,新的QName(“ http:// ...”,“ SWS”));

Note : SWS is the main class generated by wsimport from my WSDL 注意:SWS是wsimport从我的WSDL生成的主要类

Posted this here a min ago, as there was also no accepted answer: Can I globally set the timeout of HTTP connections? 一分钟前将其张贴在这里,因为也没有公认的答案: 我可以全局设置HTTP连接的超时时间吗?

For App Engine with JAX-WS you have to set the request context (tested today with SDK 1.9.15). 对于具有JAX-WS的App Engine,您必须设置请求上下文(今天已通过SDK 1.9.15测试)。 For normal machines you cannot go higher than 60s and would have to switch to the bigger machines (Bx) for better use a task queue. 对于普通计算机,您不能超过60s,并且必须切换到较大计算机(Bx)才能更好地使用任务队列。

For local testing you would normally use BindingProviderProperties.CONNECT_TIMEOUT and BindingProviderProperties.REQUEST_TIMEOUT, but they are not on the App Engine JRE White List and your code inspection might constantly warn you about that. 对于本地测试,您通常会使用BindingProviderProperties.CONNECT_TIMEOUT和BindingProviderProperties.REQUEST_TIMEOUT,但它们不在App Engine JRE白名单中,并且您的代码检查可能会不断向您发出警告。 The equivalent strings can be used though: 可以使用等效的字符串:

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

For deployment to App Engine: 部署到App Engine:

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

A full example how to apply that to auto-generated code from JAX-WS 2.x, values have to be provided in milliseconds: 一个完整的示例,如何将其应用于从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;
}

This question had gone without an upvoted answer for a long time, and although the other answer is good, I want to raise two issues which take us outside the scope of this seemingly neat-and-tidy Q&A: 很长一段时间以来,这个问题都没有一个被否决的答案,尽管另一个答案很好,但是我想提出两个问题,使我们超出了看似整洁的问答的范围:

  1. The issue of "changing the default timeout for web services calls on App Engine" depends what we mean by a web services call. “更改App Engine上的Web服务调用的默认超时”的问题取决于我们所说的Web服务调用的含义。 There are many services within the Google Cloud Platform, within the wider web itself, and then there are the Java-specific notion of WSDL. 广泛的网络本身在Google Cloud Platform中提供了许多服务,然后是WSDL的Java特定概念。 All of these will have different methods, found in the relevant documentation, for changing timeout deadlines. 在相关文档中,所有这些方法都有不同的方法来更改超时期限。

  2. There have been cases where setting deadlines was not working due to an issue on the platform, as reported in the Public Issue Tracker for App Engine . App Engine公共问题跟踪器中所述,在某些情况下,由于平台上的问题而无法设置截止日期。 If anybody reading this answer is experiencing a similar issue, feel free to open a Defect Report there, and we will quickly respond and attempt to fix an issue if one exists. 如果阅读此答案的任何人都遇到类似的问题,请随时在此处打开“缺陷报告”,如果存在问题,我们将迅速做出答复并尝试解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM