Java GAE DeferredTask 示例? [英] Java GAE DeferredTask example?

查看:17
本文介绍了Java GAE DeferredTask 示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Java DeferredTask 的文档有些困惑.我在这里阅读了 Python 文档:http://code.google.com/appengine/文章/deferred.html 但我不清楚我将如何使用 Java 版本.

I'm a bit confused by the documentation for Java DeferredTask. I've read the Python documentation here: http://code.google.com/appengine/articles/deferred.html but I'm unclear on exactly how I'd use the Java version.

您能否提供启动 DeferredTask 以使用 DatastoreService 进行简单写入的工作示例代码?

Can you provide working sample code that launches a DeferredTask to do a simple write using a DatastoreService?

推荐答案

要使用 deferred,首先必须定义一个包含要运行的代码的类:

To use deferred, you first have to define a class that contains the code you want to run:

class MyDeferred implements DeferredTask {
    @Override
    public void run() {
        // Do something interesting
    }
};

就像任何其他可序列化类一样,您可以拥有存储有关任务的相关信息的本地人.然后,要运行任务,请实例化您的类的实例并将其传递给任务队列 API:

Just like any other serializable class, you can have locals that store relevant information about the task. Then, to run the task, instantiate an instance of your class and pass it to the task queue API:

MyDeferred task = new MyDeferred();
// Set instance variables etc as you wish
Queue queue = QueueFactory.getDefaultQueue();
queue.add(withPayload(task));

您甚至可以为您的任务使用匿名内部类,但要注意注释 此处.

You can even use anonymous inner classes for your tasks, but beware of the caveats described in the note here.

这篇关于Java GAE DeferredTask 示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆