简体   繁体   中英

how akka ActorSystem process receives from actor

how can I make a process create Actor with ActorOf and allows actor to ping the parent process. My purpose is to allow an actor to send a signal to its parent process (ActorSystem main thread) to call system.terminate. But no clue how to do this. Here is snipit, but instead, I don't want to call terminate from the main, but rather get a signal from actor to call terminate. Is this doable?

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

I just figure out myself. I will simply pass the ActorSystem to the actor, then in the actor, it will call system.terminate. then it will exit the parent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM