在 sbt 0.13 项目中,我将以下内容放在位于 src/main/resources
下的application.conf
文件中
应用程序:
blocking-dispatcher {
type = PinnedDispatcher
executor = "thread-pool-executor"
thread-pool-executor {
core-pool-size-min = 2
core-pool-size-factor = 2.0
core-pool-size-max = 10
}
throughput = 100
mailbox-capacity = -1
mailbox-type =""
}
现在,当我创建演员时,我得到异常:
object Main extends App {
implicit val system = ActorSystem()
val fileReaderActor = system.actorOf(Props(new FileReaderActor(fileName)).withDispatcher("blocking-dispatcher"), "fileReaderActor")
}
我得到:
Exception in thread "main" akka.ConfigurationException: Dispatcher [blocking-dispatcher] not configured for path akka://default/user/fileReaderActor
at akka.actor.LocalActorRefProvider.actorOf(ActorRefProvider.scala:714)
at akka.actor.dungeon.Children$class.makeChild(Children.scala:191)
at akka.actor.dungeon.Children$class.attachChild(Children.scala:42)
at akka.actor.ActorCell.attachChild(ActorCell.scala:338)
at akka.actor.ActorSystemImpl.actorOf(ActorSystem.scala:518)
at Main$delayedInit$body.apply(Main.scala:14)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.collection.immutable.List.foreach(List.scala:318)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
at scala.App$class.main(App.scala:71)
at Main$.main(Main.scala:8)
at Main.main(Main.scala)
我错过了什么?
首先,确保加载您的配置:
System.out.println(system.settings());
// this is a shortcut for system.settings().config().root().render()
在这里阅读更多关于它的信息: http://doc.akka.io/docs/akka/2.2.3/general/configuration.html#Logging_of_Configuration
其次,以下配置实际上没有意义:
blocking-dispatcher {
type = PinnedDispatcher
executor = "thread-pool-executor"
thread-pool-executor {
core-pool-size-min = 2 <----- Since you're using a PinnedDispatcher, it only uses 1 thread
core-pool-size-factor = 2.0 <----- same here
core-pool-size-max = 10 <----- same here
} <--- PinnedDispatcher will automatically make it 1 thread: https://github.com/akka/akka/blob/master/akka-actor/src/main/scala/akka/dispatch/PinnedDispatcher.scala#L27
throughput = 100
mailbox-capacity = -1
mailbox-type =""
}
您必须像这样查找执行上下文调度程序
implicit val executionContext = system.dispatchers.lookup("blocking-dispatcher")
然后将此执行上下文传递给 ActorSystem。
引用文档: If an ActorSystem is created with an ExecutionContext passed in, this ExecutionContext will be used as the default executor for all dispatchers in this ActorSystem. If no ExecutionContext is given, it will fallback to the executor specified in akka.actor.default-dispatcher.default-executor.fallback
[1]
[1] http://doc.akka.io/docs/akka/snapshot/scala/dispatchers.html