GWT,Google App Engine,TimerTask或ServiceImpl中的线程抛出异常 [英] GWT, Google App Engine, TimerTask or Thread in ServiceImpl throw exception

查看:129
本文介绍了GWT,Google App Engine,TimerTask或ServiceImpl中的线程抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GWT和Google App Engine。我有一些记录,我想每30分钟更新一次。
ServiceImpl 中,我有下面的代码:

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.

我的问题是如何让一个可以在GWT + AppEngine Service?

推荐答案

不能使用java.util.Timer,因为它创建了一个普通的线程,在AppEngine上不允许。但是,您可以使用AppEngine Modules API的后台线程工具,如下所述:
https://developers.google.com/appengine/docs/java/modules/#Java_Background_threads

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

请注意,您只能使用后台线程在手动缩放的实例上。在这个例子中,后台线程每30秒打印一条消息,直到:

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();

这篇关于GWT,Google App Engine,TimerTask或ServiceImpl中的线程抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆