java - 在Java中设置Actor系统的默认ExecutionContext

标签 java akka

我想在创建 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/

相关文章:

java - 在j2me中哪里可以看到derby数据库中存储的数据

java - Maven 从源代码安装 Hadoop,寻找 tools.jar 的错误路径

java - 使用 Alpakka 手动确认 ActiveMQ 消息

scala - 在构建FlowGraph时,如何连接两个Flow?

scala - 使用 Akka、SQS 和 Camel 的消费者投票率

java - 在 java 中,如何等待多个 `Conditions`,直到其中任何一个被发出信号

java - 如何使用网络服务中的新数据更新图表(anyChart Android)?

Java Spring MVC 集成测试创建 OAuth2 主体

java - 如何在 Java 中将元素发送到 Source.actorRef 或 Source.queue

scala - 在 tomcat 7 上使用 akka 部署喷雾,无法从 Rest API 获得响应