how akka ActorSystem process receives from actor

49 Views Asked by At

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

There are 1 best solutions below

0
On

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.