【问题标题】:I can't get a child actor to receive messages我无法让儿童演员接收消息
【发布时间】:2016-10-01 12:34:53
【问题描述】:

我是 akka 的新手,我想知道如何让子 actor 接收消息。

我有一个父actor打印出它收到一条消息,然后发送一条消息给它的孩子,然后它打印出它收到一条消息。

当我运行程序时,只有父actor打印它的消息。有没有 我错过了什么?

父演员

class TestDirector(name: String) extends Actor {
  import TestDirector._

  implicit val timeout = Timeout(5.seconds)

  private var child: ActorRef = _

  override def preStart(): Unit = {
    println(name + " pre-start")
    child = context.actorOf(props("test-file"), name = "child-actor")
  }

  override def receive: Receive = {
    case TestDirectory(dir) =>
      println("Test Director")
      child ! TestWorker.TestFile
  }

}

object TestDirector {
  case class TestDirectory(dirName: String)
  case class TestResponse(message: String)

  def props(dirName: String) = Props(classOf[TestDirector], dirName)

}

童星

class TestWorker(fileName: String) extends Actor {
  import TestWorker._

  override def receive: Receive = {
    case TestFile => println("Hello world")
  }

}

object TestWorker {
  case object TestFile

  def props(fileName: String) = Props(classOf[TestWorker], fileName)
}

【问题讨论】:

    标签: scala akka


    【解决方案1】:

    在TestDirector.preStart中,你说

        child = context.actorOf(props("test-file"), name = "child-actor")
    

    那些“道具”是 TestDirector.props,您在上面导入了几行,因此您无休止地创建新的 TestDirector。我认为您想要的是 TestWorker.props:

        child = context.actorOf(TestWorker.props("test-file"), name = "child-actor")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多