gpt4 book ai didi

java - 在 AppEngine 上使用 Java Executor 导致 AccessControlException

转载 作者:搜寻专家 更新时间:2023-11-01 01:19:39 25 4
gpt4 key购买 nike

如何让 java.util.concurrent.Executor 或 CompletionService 在 Google AppEngine 上运行?类(class)都是officially white-listed ,但我在尝试提交异步任务时收到运行时安全错误。

代码:

    // uses the async API but this factory makes it so that tasks really
// happen sequentially
Executor executor = java.util.concurrent.Executors.newSingleThreadExecutor();
// wrap Executor in CompletionService
CompletionService<String> completionService =
new ExecutorCompletionService<String>(executor);
final SomeTask someTask = new SomeTask();
// this line throws exception
completionService.submit(new Callable<String>(){
public String call() {
return someTask.doNothing("blah");
}
});
// alternately, send Runnable task directly to Executor,
// which also throws an exception
executor.execute(new Runnable(){
public void run() {
someTask.doNothing("blah");
}
});
}

private class SomeTask{
public String doNothing(String message){
return message;
}
}

异常(exception):

java.security.AccessControlException: access denied (java.lang.RuntimePermission modifyThreadGroup) at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323) at java.security.AccessController.checkPermission(AccessController.java:546) at java.lang.SecurityManager.checkPermission(SecurityManager.java:532) at com.google.appengine.tools.development.DevAppServerFactory$CustomSecurityManager.checkPermission(DevAppServerFactory.java:166) at com.google.appengine.tools.development.DevAppServerFactory$CustomSecurityManager.checkAccess(DevAppServerFactory.java:191) at java.lang.ThreadGroup.checkAccess(ThreadGroup.java:288) at java.lang.Thread.init(Thread.java:332) at java.lang.Thread.(Thread.java:565) at java.util.concurrent.Executors$DefaultThreadFactory.newThread(Executors.java:542) at java.util.concurrent.ThreadPoolExecutor.addThread(ThreadPoolExecutor.java:672) at java.util.concurrent.ThreadPoolExecutor.addIfUnderCorePoolSize(ThreadPoolExecutor.java:697) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:652) at java.util.concurrent.Executors$DelegatedExecutorService.execute(Executors.java:590) at java.util.concurrent.ExecutorCompletionService.submit(ExecutorCompletionService.java:152)

此代码在 Tomcat 上或通过命令行 JVM 运行时运行良好。但是,它在 AppEngine SDK Jetty 容器中阻塞(尝试使用 Eclipse 插件和 maven-gae-plugin)。

AppEngine 可能被设计为不允许潜在危险的程序运行,因此我可以看到它们完全禁用线程创建。但是,为什么 Google 允许您创建一个类,但不允许您调用它的方法?将 java.util.concurrent 列入白名单具有误导性。

是否有其他方法可以在 GAE 上执行并行/同时/并发任务?

最佳答案

App Engine Java Overview

an app cannot spawn threads

这在他们关于 The Java Servlet Environment 的文档中有更明确的说明。

Threads

A Java application cannot create a new java.lang.ThreadGroup nor a new java.lang.Thread. These restrictions also apply to JRE classes that make use of threads. For example, an application cannot create a new java.util.concurrent.ThreadPoolExecutor, or a java.util.Timer. An application can perform operations against the current thread, such as Thread.currentThread().dumpStack().

也许白名单是为了让您可以使用接受 Executors 的库,并且您可以提供自己的 Executor 在当前线程中执行工作。

您可以试试实验性的 Task Queues

关于java - 在 AppEngine 上使用 Java Executor 导致 AccessControlException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2664738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com