Java GAE DeferredTask example?
I'm a bit confused by the documentation for Java DeferredTask. I've read the Python documentation here: http://code.googl开发者_运维知识库e.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.
The Java deferred library is still not in the GAE SDK and that's why you can't find any Official documentation.
This feature request is fixed since March 2011 and you can now use the deferred library straight from the Sdk
You could use the Vince Bonfanti deferred library that is available here.
The library usage is fairly simple and it is well explained in the doc:
1) The deferred task handler (servlet) needs to be configured within
web.xml
.
Note that the init-param must match the actual url-pattern:
<servlet>
<servlet-name>Deferred</servlet-name>
<servlet-class>com.newatlanta.appengine.taskqueue.Deferred</servlet-class>
<init-param>
<param-name>url-pattern</param-name>
<param-value>/worker/deferred</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Deferred</servlet-name>
<url-pattern>/worker/deferred</url-pattern>
</servlet-mapping>
2) The "deferred" queue needs to be configured within
queue.xml
(use whatever rate you want).
If you use the optional queue name in thedefer()
method, create queues with the appropriate names.
<queue>
<name>deferred</name>
<rate>10/s</rate>
</queue>
3) Create a class that implements the
com.newatlanta.appengine.taskqueue.Deferred.Deferrable interface
;
thedoTask
method of this class is where you implement your task logic.4) Invoke the
Deferred.defer
method to queue up your task:
DeferredTask task = new DeferredTask(); // implements Deferrable
Deferred.defer( task );
If the task size exceeds 10KB, the task options are stored within a datastore entity, which is deleted when the task is executed.
Your
doTask
method can throw aPermanentTaskFailure
exception to halt retries; any other exceptions cause the task to be retried.
Couple of bonus links:
- Feature request here.
- Google groups thread here.
- Github Fork
精彩评论