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.
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.xmlfile: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).