如何在 Google App Engine 中使用模块并使用任务队列 (Java) 向它们添加目标?

How to use Modules in Google App Engine and add a target to them using Task Queue (Java)?

我有一个任务超过了任务队列的最后期限 10 分钟以上。通过不同的文档,我发现使用模块我可以 运行 一个实例来处理长 运行ning 任务,但最好甚至应该使用任务队列来完成。我曾经使用过后端,但它们已被弃用。

我的问题是如何将模块引入我现有的 App Engine 项目以及如何将它们用于 运行 长期 运行 宁任务?

以下是一段代码:

Queue queue = QueueFactory.getQueue("myqueue");
TaskOptions task = TaskOptions.Builder.withUrl("/submitworker").method(Method.POST);
queue.add(task);

我必须在上面的代码中做哪些更改才能使用模块添加长运行ning 任务? ["submitworker" 是一个 servlet,它是实际的长 运行ning 任务]

我已经提到 this link,但我无法完成第三步:
3。将服务声明元素添加到 appengine-application.xml 文件。

此外,即使我已成功将一个模块添加到我的项目中,我如何使用任务队列定位该模块?

我已经完成了 this 问题,但它是一个 python 实现,我的实现在 Java.

我正在寻找有关如何在模块中使用 "Target" 以及如何在添加到任务队列时使用它的分步过程。

即使我将long-运行ning模块目标添加到任务队列中,它仍然会在10分钟后终止执行还是即使任务队列中的任务到期也会完成任务?

求推荐。

免责声明:答案完全基于文档(我实际上使用的是 python - 相同的概念但不同的配置)。

要使 service/module 允许长时间 运行 任务,您必须将其配置为基本或手动缩放。来自 Scaling types and instance classes(table 中的 Deadline 行):

  • Manual scaling列中:

Requests can run indefinitely. A manually-scaled instance can choose to handle /_ah/start and execute a program or script for many hours without returning an HTTP response code. Tasks can run up to 24 hours.

  • Basic scaling列中:

Same as manual scaling.

通过相应模块的 appengine-web.xml 文件完成的模块缩放配置在 Scaling elements:

中进行了描述
  • <manual-scaling>:

Optional. The element enables manual scaling for a module and sets the number of instances for a module.

  • <basic-scaling>:

Optional. The element sets the number of instances for a module.

至于实际转换为模块,用 Configuration Files (includes an example) and the appengine-web.xml Syntax 补充您指向的指南(参见 moduleservice 配置)。

关于appengine-application.xml,来自Configuration Files

The META-INF directory has two configuration files: appengine-application.xml and application.xml. The appengine-application.xml file contains general information used by App Engine tools when your app is deployed...

...

Note that while every appengine-web.xml file must contain the <application> tag, the name you supply there is ignored. The name of the application is taken from the <application> tag in the appengine-application.xml file.

要将某个队列定向到某个 service/module,您可以使用 queue.xml 文件。来自 Syntax:

  • <target>(推送队列):

Optional. A string naming a module/version, a frontend version, or a backend, on which to execute all of the tasks enqueued onto this queue.

The string is prepended to the domain name of your app when constructing the HTTP request for a task. For example, if your app ID is my-app and you set the target to my-version.my-service, the URL hostname will be set to my-version.my-service.my-app.appspot.com.

If target is unspecified, then tasks are invoked on the same version of the application where they were enqueued. So, if you enqueued a task from the default application version without specifying a target on the queue, the task is invoked in the default application version. Note that if the default application version changes between the time that the task is enqueued and the time that it executes, then the task will run in the new default version.

If you are using modules along with a dispatch file, your task's HTTP request might be intercepted and re-routed to another module.

模块和服务是一回事,它们类似于旧后端(仍然有效,但已弃用)。

有两种使模块工作的基本方法:

  • 创建一个 EAR 并部署它
  • 将服务独立部署为 WAR 文件(这可能是您现在对默认模块所做的)

第二个选项可能更容易,因为它只是更改您的应用程序的问题-web.xml。每个模块可以有一个回购或分支,或者只是一个构建过程来更改您的目标模块。

现在你的应用程序-web.xml可能有这样的东西:

<application>@appId@</application>
<version>@appVersion@</version>    
<module>default</module>   

改成这样

<application>@appId@</application>
<version>@appVersion@</version>    
<module>long-running-service</module>
<instance-class>B1</instance-class>
<manual-scaling>
    <instances>1</instances>
</manual-scaling>

您将队列本身配置为以 queue.xml 中的特定模块为目标请参阅 here