Java GAE DeferredTask example?

前端 未结 2 933
离开以前
离开以前 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.

提交回复
热议问题