Java GAE DeferredTask示例? [英] Java GAE DeferredTask example?
问题描述
您能否提供可运行的示例代码来启动DeferredTask使用DatastoreService进行简单写入?
要使用延迟,您首先必须定义一个包含代码的类想要运行:
class MyDeferred implements DeferredTask {
@Override
public void run(){
//做一些有趣的事情
}
};
就像任何其他可序列化类一样,您可以让本地人存储有关任务的相关信息。然后,运行任务,实例化您的类的一个实例,并将它传递给任务队列API:
MyDeferred task = new MyDeferred();
//根据需要设置实例变量等
队列队列= QueueFactory.getDefaultQueue();
queue.add(withPayload(task));
你甚至可以为你的任务使用匿名内部类,但要注意注释< href =http://download.oracle.com/javase/6/docs/platform/serialization/spec/serial-arch.html#4539 =noreferrer> here 。
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.
Can you provide working sample code that launches a DeferredTask to do a simple write using a DatastoreService?
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
}
};
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屋!