views:

1407

answers:

3

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 ?

+5  A: 

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

Chii
Thanks :) I will check it.
JOKe
A: 

See also task queues as an alternative.

Barnabas Kendall
A: 

The issue is not just in App engine, but in general in any Servlet container. When one is in a service method (which you are always in Servlet container), you can not create threads and do sleeps.

In today's scalable service world, thread.sleep is a bad thing....

mak
This is almost entirely wrong. In a normal Servlet container it is perfectly possible/desirable to create background threads which perform periodic actions. You are correct in saying that Thread.sleep is bad on the request thread, but not on a background thread.
Geoff