我是akka的新手,我想知道如何让儿童演员接收消息。
我有一个父演员打印出它接收到一条消息,然后向其子节点发送消息,然后打印出它收到的消息。
当我运行程序时,只有父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)
}
答案 0 :(得分:0)
在TestDirector.preStart中,您说
child = context.actorOf(props("test-file"), name = "child-actor")
那些"道具"是Test Director .props,您在上面输入了几行,因此您无休止地创建新的TestDirector。我想你想要的是Test Worker .props:
child = context.actorOf(TestWorker.props("test-file"), name = "child-actor")