【发布时间】:2020-08-17 23:07:09
【问题描述】:
免责声明:我只使用 akka 来获取调度程序和底层 ExecutionContextExecutor。我没有使用任何 akka 演员。我查找在配置文件 application.conf 中定义的自定义调度程序,然后在我的 scala 应用程序中使用它,用于未来和并行集合。
伪应用程序.conf:
akka {
my-executor {
...
}
}
现在,我对 akka 文档中的以下行感到困惑:
查找调度程序
implicit val executionContext = system.dispatchers.lookup("my-dispatcher")
https://doc.akka.io/docs/akka/2.5.24//dispatchers.html
system (ActorSystem) 指的是哪个知道 application.conf 中定义的所有执行器配置或能够从中创建调度程序?
因为我尝试了以下操作但失败了:
implicit val actorSysterm = ActorSystem()
actorSysterm.dispatchers.lookup("akka.my-executor") //Error: akka.ConfigurationException: Dispatcher [akka.my-executor] not configured
【问题讨论】:
-
调度程序定义在 akka 大括号之外。如果你不使用actor,那么使用它真的没有意义。它使用 Java 执行器作为底层实现,因此它们的性能相似。您可以使用
ExecutorContext.fromExecutor将 Java 执行器直接传递到 ScalaFuture隐式 ExecutorContext 参数 -
顺便说一句,如果它在 akka 外部定义,那么为什么无法访问它?正如我提到的那样,文档假设您的范围内有
system,我没有这样做,因为我不使用演员 -
没有在大括号外定义,但您仍然可以使用
lookup访问它