Scala akka: do the actor remember what he should finally answer?

in akka, suppose there is a linear chain of actors, so that each actor receives a message from the upstream, sends its own message downstream and waits for a response, and then sends the message back. how can an actor remember the handle of an upstream actor when he needs to respond to that actor later?

eg:

A sends message to B (b ! "msg1") B sends message to C (c ! "msg2") C replies to B (self.reply ! "msg3") B replies to A <--- ??? 

Basically, how can B remember the handle for A? doing self.reply at this point will refer to C, since C sent the current message to B.

+4
source share
1 answer

Should actor B change the response message between C and A?

  • If not, actor B should use B forward "msg" instead of B ! "msg" B ! "msg" . This will save the sender information. When C uses the response, the response is automatically sent to without passing through B:

    A sends message to B (b ! "msg")

    B forward the message to C (c forward "msg")

    C replies to A (self.reply ! "msg3")

  • If so, get the sender of ActorRef and pass it by the link of the sender of the message.

For example, using a simple tuple:

 A sends message to B (b ! "msg1") B sends message to C, with the sender reference (c ! ("msg2",self.sender) ) C replies to B adding the reference it received (self.reply ! ("msg3",ref) ) B replies to A ( ref ! "msg4" ) 

I used Tuple here, but if you have a long chain, go List[ActorRef] . As you move forward, you add senders refs. And when you go back, you respond to the head of the list and pass the tail of the list by answer.

Edit: Considering Victor's comment.

+6
source

All Articles