使用Akka Dispatchers处理期货

时间:2016-05-03 11:14:08

标签: scala akka akka-dispatcher

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

1 个答案:

答案 0 :(得分:1)

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

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