Let's say I have actor A. A expects to receive message and once it receives a message it sends two messages back.
A extends Actor {
def receive: Receive = {
case M1 =>
context.sender ! M2
context.sender ! M3
}
}
And in actor A I want to send a message and then await for two responses. I know that's easy for one response in a way like
val task = A ? M1
Await.result(task, timeout)
but I'm not sure whether it is possible with two sequential messages.
It is important to send two separate messages as I need to Await only first of them in another place.
You can solve this problem by introducing an intermediate Actor in the cases when you do need to wait for both messages.
This actor would look something like this:
Essentially it has internal state and keeps track of how
M1
andM2
responses are arriving.You could use this like: