GWT, Google App Engine, TimerTask or Thread in Ser

2019-02-22 01:30发布

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条回答
时光不老,我们不散
2楼-- · 2019-02-22 02:09

See also task queues as an alternative.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-02-22 02:11

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();
查看更多
看我几分像从前
4楼-- · 2019-02-22 02:16

You sir, could benefit from appengine's newish ThreadManager class.

https://developers.google.com/appengine/docs/java/backends/overview#background_threads

Use ThreadManager.currentRequestThreadFactory() to get a Thread factory which will interrupt spawned threads as soon as the current request is finished, and ThreadManager.backgroundThreadFactory() to spawn threads that will last as long as you please (but only works on backends)

For frontend requests, it is recommended to keep track of your threads, and call thread.join() if you want them to finish before appengine's request filter interrupts them.

查看更多
你好瞎i
5楼-- · 2019-02-22 02:27

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....

查看更多
仙女界的扛把子
6楼-- · 2019-02-22 02:29

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

查看更多
登录 后发表回答