Akka调度程序继承

11

我正在使用Akka,我们还在相互了解中。

我的情境是:我为一个管理(监督)并创建子Actor来完成工作的主管(父级)Actor选择了一个非默认的调度器。

问题子Actor会继承父Actor吗?

我知道您可以在配置中指定不同的调度器,从而明确地为子Actor和父Actor指定不同的调度器。

akka.actor.deployment {
  /my-parent-actor {
    dispatcher = dispatcher-for-parent
  }

  "/my-parent-actor/*" {
    dispatcher = dispatcher-for-children
  }
}

如果您指定了父级Actor调度程序,而没有明确为子Actor指定调度程序,那么父级Actor的调度程序是否会被继承到其子Actor呢?

1个回答

17

根据我的观察,子元素默认情况下不会继承父元素的supervisor。我在文档中没有找到明确的说明,因此我编写了一小段代码来验证我的初步假设:

import com.typesafe.config.ConfigFactory
import akka.actor._

object DispatcherTest extends App{

  val conf = ConfigFactory.parseString("""
      {
          my-custom-dispatcher {
            executor = "thread-pool-executor"
            type = PinnedDispatcher         
          }
      }  
  """)

  val system = ActorSystem("test", conf)
  val supervisor = system.actorOf(Props[MySupervisor].withDispatcher("my-custom-dispatcher"))

}

class MySupervisor extends Actor{
  println(s"I am the supervisor, my dispatcher is: ${context.dispatcher}")
  val child = context.actorOf(Props[MyChild])
  def receive = {
    case _ =>      
  }
}

class MyChild extends Actor{
  println(s"I am the child, my dispatcher is: ${context.dispatcher}")
  def receive = {
    case _ =>
  }
}

如果你运行这个程序,你将会看到:

I am the supervisor, my dispatcher is: PinnedDispatcher[my-custom-dispatcher]
I am the child, my dispatcher is: Dispatcher[akka.actor.default-dispatcher]

非常感谢。回答很棒!在这个相互了解的阶段,与Akka相关联,能够将我看到的与其他人得到的结果进行比较,真的很好。再次感谢!! - neurozen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接