0

我有一个基于喷雾的 HTTP 服务。我有一个在这个 HTTP 应用程序中运行的流。现在因为这个流做了很多 I/O,我决定使用一个单独的线程池。我查阅了 Akka 文档,看看我能做些什么来使我的线程池是可配置的。我在 Akka 中遇到了 Dispatcher 概念。所以我尝试在我的 application.conf 中使用它,如下所示:

akka {
  io-dispatcher {
    # Dispatcher is the name of the event-based dispatcher
    type = Dispatcher
    # What kind of ExecutionService to use
    executor = "fork-join-executor"
    # Configuration for the fork join pool
    fork-join-executor {
      # Min number of threads to cap factor-based parallelism number to
      parallelism-min = 2
      # Parallelism (threads) ... ceil(available processors * factor)
      parallelism-factor = 2.0
      # Max number of threads to cap factor-based parallelism number to
      parallelism-max = 10
    }
    # Throughput defines the maximum number of messages to be
    # processed per actor before the thread jumps to the next actor.
    # Set to 1 for as fair as possible.
    throughput = 20
  }
}

在我的 Actor 中,我尝试将此配置查找为:

context.system.dispatchers.lookup("akka.io-dispatcher")

当我运行我的服务时,我收到以下错误:

[ERROR] [05/03/2016 12:59:08.673] [my-app-akka.actor.default-dispatcher-2] [akka://my-app/user/myAppSupervisorActor] Dispatcher [akka.io-dispatcher] not configured
akka.ConfigurationException: Dispatcher [akka.io-dispatcher] not configured
    at akka.dispatch.Dispatchers.lookupConfigurator(Dispatchers.scala:99)
    at akka.dispatch.Dispatchers.lookup(Dispatchers.scala:81)

我的问题是:

  1. 我创建的这个 io-dispatcher 线程池是否仅用于 Actor 的?我的意图是将此线程池用于我的流,该流由其中一个 Actor 实例化。然后我将此线程池传递给我的流。

  2. 我如何通过从 application.conf 加载调度程序来创建 ExecutionContext?我是否应该使用任何特定的库来读取我的线程池配置并给我一个 ExecutionContext?

4

1 回答 1

1

配置正确。您需要做的就是将加载的配置文件传递给 Akka ActorSystem,例如:

ActorSystem("yourActorSystem", ConfigFactory.load())
于 2016-05-03T12:44:47.867 回答