阿卡和测试套件。无法获取演员 children

akka and testkit. Can't get actor children

我有一个具有接收方法的演员:

def receive: Actor.Receive = {
    case Tick =>
        val child = context.system.actorOf(...) // create actor
        context.watch(child)
        child ! something

    case AskRunningJobs =>
      log.info(s"My children: ${context.children.toList.length}")
      log.info(s"My children: ${context.children.toList.map(_.path.toSerializationFormat).mkString(" ||| ")}")
      sender ! RunningJobs(context.children.toList.length)

    case unknown =>
      log.warning(s"unknown message: $unknown")
  }

我有详细的日志输出,我可以清楚地看到创建了children,它们是运行。但是

context.children.toList.length

始终为零。为什么? 我是 运行 我的演员,正在使用 TestKit。

通过这种方式创建children

val child = context.system.actorOf(...) // create actor

你让创建的角色成为监护人 children(即你失去了上下文)。只有您的 top-level 演员应该以这种方式创建。

要让他们children成为你的演员,你需要使用

val child = context.actorOf(...) // create actor

相反。有关 actor 创建的更多信息,请参见 docs.