Question

I'm new to Actors and I'm trying to understand how to chain actors properly. I've read Ask: Send-And-Receive-Future section of documentation.

Here is how I did it now

case class Message1(text : String)

class RootActor extends Actor {
  def receive: Actor.Receive = {
    case Message1(message) => {
      (context.actorOf(Props( new TestActor1)) ? message)(5.seconds) pipeTo sender
    }
  }
}

class TestActor1 extends Actor {
  def receive: Actor.Receive = {
    case message : String => {
      sender ! (message + " TestActor1")
    }
  }
}

object Test {
  def main(args: Array[String]) {
    println("Start!")

    implicit val system = ActorSystem()
    val rootActor = system.actorOf(Props( new RootActor))

    for (reply1 <- (rootActor ? Message1("Begin"))(5.seconds).mapTo[String])
      println(reply1)
  }
}

So I manually relay reply. But shouldn't replies from child actor bubble up to its parent automatically ? Is this right way of doing it ?

Also I'm concerned that in manual way of bubbling up reply I use ask which requires timeout. But what if some child actor requires more time to do its job than others but I'm limited by one timeout value when I send message to root actor ? It seems to me that maintaining proper timeouts across entire chain is very cumbersome.

How can I simplify it ?

Was it helpful?

Solution

Just use forward instead. That's what it's there for. Use it like so :

context.actorOf(Props( new TestActor1)).forward(message)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top