如何让一个进程使用 ActorOf 创建 Actor 并允许 Actor ping 父进程。我的目的是允许演员向其父进程(ActorSystem 主线程)发送信号以调用 system.terminate。但不知道如何做到这一点。这是 snipit,但相反,我不想从 main 调用 terminate,而是从 actor 获得一个信号来调用 terminate。这是可行的吗?
object MyTest {
def main(args: Array[String]): Unit = {
println("test test")
Thread.sleep(10000)
val system = ActorSystem("HelloSystem")
system.actorOf(TestActor.props("testactor"), name = "testactor")
system.terminate()
}
}
object TestActor {
def props(conf: String): Props = Props(new TestActor(conf))
case class AnswerMe(txt: String)
}
class TestActor(conf: String) extends Actor {
import TestActor._
override def receive: PartialFunction[Any, Unit] = {
case AnswerMe(txt) => {
println(s"$txt")
?? ! "answer"
}
}
}