Java GAE DeferredTask example?

I am a bit confused by the documentation for Java DeferredTask. I read the Python documentation here: http://code.google.com/appengine/articles/deferred.html , but I don’t quite understand exactly how I will use the Java version.

Can you provide a working example of code that runs DeferredTask for simple recording using the DatastoreService?

+7
source share
2 answers

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

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

Like any other serializable class, you can have local resources that store relevant information about the task. Then, to start the task, create 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 .

+13
source

The deferred Java library is still not in the GAE SDK and why you cannot find any official documentation. Strike > This function request has been fixed since March 2011, and now you can use the deferred library directly from Sdk

You can use the Vince Bonfanti deferred library available here .

Using the library is quite simple and well explained in the document:

1) It is required to configure the pending task handler (servlet) within web.xml .
Note that 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 must be configured within queue.xml (use no matter which one you want).
If you use the optional queue name in the defer() method, create queues with the appropriate names.

  <queue> <name>deferred</name> <rate>10/s</rate> </queue> 

3) Create a class that implements com.newatlanta.appengine.taskqueue.Deferred.Deferrable interface ;
The doTask method of this class is the place where you perform your logic task.

4) Call the Deferred.defer method to queue your task:

  DeferredTask task = new DeferredTask(); // implements Deferrable Deferred.defer( task ); 

If the task size exceeds 10 KB, the task parameters are stored within the datastore, which is deleted when the task is performed.

Your doTask method may throw a PermanentTaskFailure exception to stop repeats; any other exceptions cause the task to retry.

A couple of bonus links:

  • Feature request here .
  • Google group groups are here .
  • Github fork
+1
source

All Articles