我正在使用GWT和Google App Engine。我有很多记录,我想每30分钟更新一次。在ServiceImpl中,我有以下代码:
new Timer().schedule(new TimerTask(){
@Override
public void run() {
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
result = updateFeeds();
}
}, 30000,Long.MAX_VALUE);
当我运行应用程序以及何时获得:
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)
在第一行或粘贴的代码处。
我的问题是,我如何才能使可以在GWT + AppEngine Service中工作的后台工作者?
您不能使用java.util.Timer,因为它会创建一个普通线程,这在AppEngine中是不允许的。但是,您可以使用AppEngine Modules API的Background线程功能,如此处所述:https : //developers.google.com/appengine/docs/java/modules/#Java_Background_threads
请注意,您只能在手动缩放的实例上使用后台线程。在此示例中,后台线程永远每30秒打印一次消息:
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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句