Java GAE DeferredTask example?

前端 未结 2 934
离开以前
离开以前 2020-12-03 12:34

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 un

相关标签:
2条回答
  • 2020-12-03 13:13

    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.

    0 讨论(0)
  • 2020-12-03 13:21

    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
    0 讨论(0)
提交回复
热议问题