Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT, Google App Engine, TimerTask or Thread in ServiceImpl throw exception

I am using GWT and Google App Engine. I have array of records and I want to update them every 30 mins. In the ServiceImpl I have the fallowing code :

new Timer().schedule(new TimerTask(){
    @Override
    public void run() {
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        result = updateFeeds();
    }
}, 30000,Long.MAX_VALUE);

When I run the application and when I get :

com.google.gwt.user.server.rpc.UnexpectedException:
Service method 'public abstract java.util.List org.joke.planet.client.FeedService.loadFeeds()' threw an unexpected exception:
java.security.AccessControlException: access denied (java.lang.RuntimePermission modifyThreadGroup)

at the first line or pasted code.

My Question is HOW I can make a background worker that works in GWT+AppEngine Service ?

like image 378
JOKe Avatar asked Aug 14 '09 09:08

JOKe


2 Answers

you cannot - a background worker implies a thread, and thread creation in gae does not work.

The answer to your task is not to create a thread or background worker, but to use this http://code.google.com/appengine/docs/java/config/cron.html

like image 93
Chii Avatar answered Oct 10 '22 21:10

Chii


You can't use java.util.Timer because it creates an ordinary thread, which is not allowed on AppEngine. However, you can use the AppEngine Modules API's Background thread facility, as documented here: https://developers.google.com/appengine/docs/java/modules/#Java_Background_threads

Note that you can only use background threads on manually scaled instances. In this example, a background thread prints out a message every 30 seconds, forever:

Thread thread = ThreadManager.createBackgroundThread(new Runnable() {
  public void run() {
    try {
      while (true) {
        System.err.println("Doing something!");
        Thread.sleep(30_000);
      }
    } catch (InterruptedException ex) {
    }
  }
});
thread.start();
like image 35
Zero Trick Pony Avatar answered Oct 10 '22 21:10

Zero Trick Pony