quarks
quarks

Reputation: 35276

Multithreading with Appengine

Since Appengine won't allow java multithreading, how then can we migrate our existing multithreaded code to the platform?

For example I have the following code:

    Thread t = new Thread() {
        public boolean alive = true;
        public void run() {
            while (alive) {
                try {
                    Thread.sleep(5000);    
                    getNewNotifications();
                } catch (InterruptedException e) {
                    //  Do nothing
                } catch (IOException e) {
                } 
            }
        }
    };
    t.start()

The function getNewNotification() does some several Rest/HTTP calls, that may include some other process that may return indefinitely. I have read the Task Queue is the solution, however how do we convert this simple code into App engine-friendly code?

How is the code above implemented using Task queue? For example to call getNewNotifications() for every five seconds.

And that function will get some results from the server, parse the result and then execute the activities/work it needs to do based on the result.

Upvotes: 4

Views: 3278

Answers (4)

Moritz Petersen
Moritz Petersen

Reputation: 13047

Depending on your budget (check billing for backends), you can achieve this also by using Scheduled Tasks.

You would specify the task in the cron.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
  <cron>
    <url>/getNewNotifications</url>
    <description>Get new notifications every 5 minutes</description>
    <schedule>every 5 minutes</schedule>
  </cron>
</cronentries>

Of course, you need to have a servlet (or whatever framework you are using) mapped to the URL /getNewNotifications.

You should also make sure, that the URL is secure (normally you don't want your users call that URL).

Upvotes: 1

Ajax
Ajax

Reputation: 2520

You can create threads in java appengine.

ThreadManager.createThreadForCurrentRequest(new Runnable(){...});

See https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/ThreadManager

A frontend thread will be interrupted and killed when the request is complete, but a spawned backend thread can run for as long as it pleases. Also, try to do more than nothing when you catch InterruptedException; swallowing this exception can cause instances to stay online and will cost you more money.

If you want to make your code work with Runnable and task queues, just implement both Runnable and DeferredTask; both interfaces have the same method signature. To dispatch a deferred task, just do QueueFactory.getQueue("queueName").add( TaskOptions.Builder.withPayload(YourDeferredTask));

Upvotes: 6

Peter Knego
Peter Knego

Reputation: 80330

Since you need your code to execute periodically I'd suggest cron.

Just put your code inside a servlet's get(..) method, map it to Url (via web.xml) and set cron to call this Url periodically.

Upvotes: 1

Eelke
Eelke

Reputation: 21993

You might also be interested in backends. Backends can run as background threads (experimental feature) so they can be used to poll like you are used to do. However if possible it probably is more efficient to use tasks. If the notifications are coming from some other part of your app you could create tasks directly instead of creating notifications.

Upvotes: 2

Related Questions