I am fairly new to Akka and went through this request-response example of the Akka documentation.
I understood this pattern works like the following:
Dave
asksHal
by sending a message ofHal
's protocol.Hal
answers by sending a message of its own protocol toDave
.Dave
, who does not knowHal
's protocol gets the response adapted and re-sent to itself.Dave
receives the adapted response and continues with its payload.
context.ask(dave, Dave.Request) {
case Failure(exception) => throw exception
case Success(response) => AdaptedResponse(response.payload)
}
But what happens, if Dave
needs response.payload
diretly on the spot? Is this even possible?
In Akka Typed, it's not possible using
context.ask
, though I would question why you specifically need it.As an alternative, when you have two actors which interact, you can define a joint-protocol between them:
Then
Dave
can justand get the response directly from
Hal
(and you can schedule a message to yourself to account for the timeout).Note that this really entangles two actors together. It's suitable for cases like where a parent actor defers a long-running task to a child actor, but it's a really questionable approach in most/all other scenarios.