Basic AppEngine Asynchronous Tasks

BY

Google’s AppEngine is a very useful platform in the way it allows developers to have an application server up and running within 10 minutes.
It leverages the Google infrastructure, too, offering high speed, high capacity, etc.

Wow (Awesome!)

However (Hmm), AE does not allow for the usual multi-threading mechanisms…

For security reasons, Google sandboxes your applications.
For resource management reasons, Google also tries to keep a hold of processes within your app. The Java Thread class is only partially supported; you can’t start a Thread, but you can put a Thread to sleep: Thread.sleep(1000);

How can I multithread with AppEngine?

AppEngine does provides a service to run tasks asynchronously.
We get a hold of a Queue onto which we can submit serialized Tasks. You can actually keep track of your queues on the AppEngine dashboard (See Left Menu -> Task Queues).
[code lang=”java”]
Queue myQueue = QueueFactory.getDefaultQueue();
myQueue.add(…);
[/code]

To create your own task, implement the DeferredTask interface.

[code lang=”java”]public class MyTask implements DeferredTask {
private static final Logger log = Logger.getLogger(My.class.getName());
public MyTask() {}

@Override
public void run() {
// Code for all your hearts content
log.info("Giants World-series Champions!");
}
}
[/code]

And then:

[code lang=”java”]MyTask myTask = new MyTask();
myQueue.add(TaskOptions.Builder.withPayload(myTask));[/code]

That’s it.

You might want to give a name to your task; that will allow you to clean up later on if you need to.
[code lang=”java”]
MyTask myTask = new MyTask();
String myVeryOwnTask = "MyVeryOwnTask"
queue.add(TaskOptions.Builder.withPayload(myTask).taskName(myVeryOwnTask);

queue.deleteTask(myVeryOwnTask);[/code]

Note: The task is serialized using the Serializable interface; make sure your members implement it as well.

This is just the beginning. There are multiple options that you can set when adding a task to a queue, like what happens when a task fails: should it retry? You can find more details about threading with AppEngine on their Google Docs.

Leave a Reply

Your email address will not be published. Required fields are marked

Your comment

Your name, please

Your email, please

POSTED

November 01, 2012

SHARE POST

LOCATED IN

(29)

TAGS

(1)
(26)
(7)
(6)
(1)

CATEGORIES

HAVE A QUESTION ABOUT OUR SERVICES?

Ready to start on a project?

WANT TO SEE OUR WORK?

Visit our case studies page or request specific project examples.