【问题标题】:akka and testkit. Can't get actor childrenakka 和 testkit。不能得到演员孩子
【发布时间】:2017-04-19 14:48:52
【问题描述】:

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

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")
  }

我有详细的日志输出,我可以清楚地看到孩子们已经创建并且正在运行。但是

context.children.toList.length

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

【问题讨论】:

    标签: scala akka akka-testkit


    【解决方案1】:

    通过这种方式创建孩子

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

    你使创建的actors成为监护人的孩子(即你失去了上下文)。只有您的顶级演员应该以这种方式创建。

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

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

    相反。有关演员创建的更多信息,请参阅docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 2012-11-28
      相关资源
      最近更新 更多