【问题标题】:how akka ActorSystem process receives from actorakka ActorSystem 进程如何从actor接收
【发布时间】:2020-04-16 22:33:35
【问题描述】:

如何让一个进程使用 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"
        }
    }
}

【问题讨论】:

  • 通常使用context.parent,但在您的情况下,父级甚至不是Actor。你可以使用context.system.terminate()

标签: scala akka actor actorsystem


【解决方案1】:

我只是弄清楚自己。我将简单地将 ActorSystem 传递给 actor,然后在 actor 中,它会调用 system.terminate。然后它将退出父级。

【讨论】:

    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多