Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java GAE DeferredTask example?

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?

like image 301
Dan Fabulich Avatar asked Sep 21 '11 18:09

Dan Fabulich


2 Answers

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.

like image 72
Nick Johnson Avatar answered Nov 09 '22 20:11

Nick Johnson


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 the defer() 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;
the doTask 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 a PermanentTaskFailure 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
like image 39
systempuntoout Avatar answered Nov 09 '22 19:11

systempuntoout