使用 Akka Dispatchers 处理期货

Using Akka Dispatchers for Handling Futures

我有一个基于 Spray 的 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 吗?

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

ActorSystem("yourActorSystem", ConfigFactory.load())