如何在自己的执行上下文中运行期货,而不是在Actor系统调度程序上运行期货.(Scala | Akka)



如果代码中的dedicated dispatcher会自动在application.conf中查找dispatcher

,那么我们如何使用src/main/resources/application.conf呢?

这里有两个执行上下文的配置示例,您可以将其放入application.conf中:

background-scheduled-tasks-dispatcher {                                                
  type = Dispatcher                                                                    
  executor = "fork-join-executor"    
  fork-join-executor {
    parallelism-min = 2
    parallelism-factor = 2.0
    parallelism-max = 10
  }
  throughput = 1
}
blocking-io-ec {
  type = Dispatcher
  executor = "thread-pool-executor"
  thread-pool-executor {
    fixed-pool-size = 50
  }
  throughput = 1
}

下面是一个如何在你的应用程序中访问它们的例子:

val ec :ExecutionContext = actorSystem.dispatchers.lookup("blocking-io-ec")

你可以在行前加上implicit val ec或者直接使用

someFuture.map(myFunc)(ec)

最新更新