有没有办法使用App Engine Java运行时动态设置任务的目标?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了有没有办法使用App Engine Java运行时动态设置任务的目标?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5516字,纯文字阅读大概需要8分钟。
内容图文
![有没有办法使用App Engine Java运行时动态设置任务的目标?](/upload/InfoBanner/zyjiaocheng/797/b6a5ccc4894e4bae98f7aed4ce4f95ba.jpg)
使用App Engine Python运行时将后台任务排入队列时,您可以使用specify a target for the queue将任务发送到特定服务,版本或实例上:
task = taskqueue.add(
url='/update_counter',
target='worker',
params={'amount': amount})
有没有办法在Java中这样做?文档mentions the target parameter但没有显示如何使用它的示例. Queue.add方法没有目标选项. TaskOptions类也没有任何看起来像目标的东西.
This question documents how to use the target,但答案是在queue.xml中配置它.我想在运行时选择目标,就像Python一样.
解决方法:
TL; DR – 根据早期版本的文档可能有一种方法可以做到这一点,但最新版本的文档中没有对此进行描述.
基于最新文档的方法
根据latest documentation regarding Push queues(和您已经提到的那样),您可以在每个队列的queue.xml中配置目标模块version.如果指定,则将任务请求发送到指定目标.正如您已经描述的那样,这是一个静态配置,并没有真正回答您的问题(仅为了完整性而描述).
<?xml version="1.0" encoding="UTF-8"?>
<queue-entries>
<queue>
<name>queue-blue</name>
<target>v2.task-module</target>
</queue>
<queue>
<name>queue-red</name>
<rate>1/s</rate>
</queue>
</queue-entries>
在documentation for creating tasks and specifying worker service中,它描述了为任务选择的目标,但没有清楚地描述如何指定它.
When a task is popped off its queue, the Task Queue service sends it
on to a worker service. Every task has a target and a url, which
determine what service and handler will ultimately perform the task.
target
The target specifies the service that will receive the HTTP request to
perform the task. It is a string that specifies a
service/version/instance in any one of the canonical forms. The most
often-used ones are:06001
The target string is prepended to the domain name of your app. There
are three ways to set the target for a task:
- Explicity declare the target when you construct the task.
- Include a target directive when you define a queue in the queue.xml, as in the definition of queue-blue above. All tasks added to a queue
with a target will use that target, even if a different target was
assigned to the task at construction time.- If no target is specified according to either of the previous two methods, then the task’s target is the version of the service that
enqueues it. Note that if you enqueue a task from the default service
and version in this manner, and the default version changes before the
task executes, it will run in the new default version.
url
The
url
selects one of the handlers in the target service, which will
perform the task.The
url
should match one of the handler URL patterns in the target
service. The url can include query parameters if the tasks’s method is
GET
orPULL
. If no url is specified the default URL
/_ah/queue/[QUEUE_NAME]
is used, where[QUEUE_NAME]
is the name of the
task’s queue.
根据上述文档,如果查看TaskOptions.Builder,则没有方法可以指定任务的目标.这可能表示缺少有关如何指定目标的文档,或者只是在将任务添加到队列时无法再动态指定目标.根据早期文档,查看下面描述的方法.
基于早期文档的方法
免责声明:我在这里提到的看起来是基于过时的信息(我引用了下面的来源),因此可能无法按预期工作和/或将来中断.
您可以使用Host标头指定将获取请求的模块,版本和实例信息.
要指定模块和版本,您可以执行以下操作:
Queue queue = QueueFactory.getQueue("QUEUE_NAME");
queue.add(TaskOptions.Builder
.withUrl("/url/path")
.param("key", "PARAM")
.header("Host",
ModulesServiceFactory.getModulesService().getVersionHostname("MODULE_NAME", "VERSION")));
要指定实例,您可以:
Queue queue = QueueFactory.getQueue("QUEUE_NAME");
queue.add(TaskOptions.Builder
.withUrl("/url/path")
.param("key", "PARAM")
.header("Host",
ModulesServiceFactory.getModulesService().getInstanceHostname("MODULE_NAME", "VERSION", "INSTANCE_NAME"));
我在App Engine文档的当前版本中找不到此信息,但是使用Wayback机器,我在earlier version of the doc from Jan 1 2016中找到了此信息,其中描述了Push任务执行.在github issue中也讨论了不同的背景.
Push task execution
App Engine executes push tasks by sending HTTP POST requests to your
app. Specifying a programmatic asynchronous callback as an HTTP
request is sometimes called a web hook. The web hook model enables
efficient parallel processing.The task’s URL determines the handler for the task and the module that
runs the handler.The handler is determined by the path part of the URL (the
forward-slash separated string following the hostname), which is
specified by the url parameter in the TaskOptions that you include in
your call to the Queue.add() method. The url must be relative and
local to your application’s root directory.The module and version in
which the handler runs is determined by:
- The “Host” header parameter
in the TaskOptions that you include in your call to the Queue.add()
method.- The target directive in the queue.xml or queue.yaml file.
If you do not specify any of these parameters, the task
will run in the same module/version in which it was enqueued, subject
to these rules:
If the default version of the app enqueues a task, the task will run
on the default version. Note that if the app enqueues a task and the
default version is changed before the task actually runs, the task
will be executed in the new default version.If a non-default version
enqueues a task, the task will always run on that same version.Note:
If you are using modules along with a dispatch file, a task’s URL may
be intercepted and re-routed to another module.The namespace in which
a push task runs is determined when the task is added to the queue. By
default, a task will run in the current namespace of the process that
created the task. You can override this behavior by explicitly setting
the namespace before adding a task to a queue, as described on the
multitenancy page.
内容总结
以上是互联网集市为您收集整理的有没有办法使用App Engine Java运行时动态设置任务的目标?全部内容,希望文章能够帮你解决有没有办法使用App Engine Java运行时动态设置任务的目标?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。