How does akka know which specific response to a message is related to the right future?

Let's say I ask (?) The same actor for two answers. He sends the sender later. He later receives messages to send to senders. We get the correct sender (he hash the message), but how does Akka know for which message the answer is? Is there something in the ActorRef that indicates for which message each response responds?

Is this a "channel"?

I would like to better understand the underlying technology. I will try to read the source at the same time, but I think this is a really good question.

Code example:

class TestActor [...] def onReceive = { case r: MessageToGoOut ⇒ messageId += 1 val requestId = clientConnectionId + messageId senders += (requestId -> sender) //store sender for later anotherActor ! WrappedUpMessage(requestId, MessageOut)) case m: MessageToGoBackToSender ⇒ val requestId = m.requestId senders.get(requestId) map { client ⇒ client ! Response(m.message) senders -= requestId } } val futures = for(i <- 1 to 100) yield testActor ? new MessageToGoOut ("HEYO!" + i) 

Now, how does akka guarantee that messages return to the correct actor?

+4
source share
1 answer

Each Actor has a path. From inside Actor you can say:

 context.path 

From outside a Actor , if you have an ActorRef , you can simply say:

 ref.path 

This way, if the address of this instance is a separate actor, and I believe that the internal routing system routes messages to mailboxes for instances of subjects. When you are outside of Actor , for example, when you execute a loop and send messages in your example, when you use ask ( ? ), A temporary instance of Actor launched, so when the Actor to which the message is received needs to be answered, it has a path for response. This probably makes things a little easier, and it may not be the level of detail you are looking for, so I apologize if I missed the point of your question.

Also, sender var in Actor is an ActorRef , so it has a path so you can get back to it.

When Future is created, akka creates a temporary (and addressable) Actor that basically serves this Future . When this temporary Actor sends another Actor , its ActorRef is passed as the sender. When the receiving actor processes this particular message, the sender var set to ActorRef for this temporary actor, that is, you have an address to respond to. Even if you decide to hold this sender at a later date, you still have an address to send back and, ultimately, complete Future , which is served by a temporary actor. The thing is, as long as you have an ActorRef , whether it is a request or a response, all that it does is direct the message to the path for that ActorRef .

Ask (?) And tell (!) Ask (?) really not very different from each other. ask is basically tell , where the sender expects the recipient to return a tell message to him.

+3
source

All Articles