我想在创建 Actor System 时将 Java 中的 ExecutorService 对象的引用作为默认调度程序/executionContext 传递。有没有办法做到这一点,而不是从配置中读取调度程序属性。
最佳答案
使用ActorSystem
工厂方法create
它采用 ExecutionContext 作为参数。以下是方法签名和 Javadoc(重点是我的):
public static ActorSystem create(java.lang.String name,
com.typesafe.config.Config config,
java.lang.ClassLoader classLoader,
scala.concurrent.ExecutionContext defaultExecutionContext)
Creates a new ActorSystem with the specified name, the specified Config, the specified ClassLoader, and the specified ExecutionContext. The ExecutionContext will be used as the default executor inside this ActorSystem. If
null
is passed in for the Config, ClassLoader and/or ExecutionContext parameters, the respective default value will be used. If no Config is given, the default reference config will be obtained from the ClassLoader. If no ClassLoader is given, it obtains the current ClassLoader by first inspecting the current threads' getContextClassLoader, then tries to walk the stack to find the callers class loader, then falls back to the ClassLoader associated with the ActorSystem class. If no ExecutionContext is given, the system will fallback to the executor configured under "akka.actor.default-dispatcher.default-executor.fallback". Note that the given ExecutionContext will be used by all dispatchers that have been configured with executor = "default-executor", including those that have not defined the executor setting and thereby fallback to the default of "default-dispatcher.executor".
例如:
import scala.concurrent.ExecutionContext;
final ExecutionContext ec = ...
ActorSystem system = ActorSystem.create("MySystem", null, null, ec);
关于java - 在Java中设置Actor系统的默认ExecutionContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51583993/