Non-blocking ask between typed actors

Hi,
I have a current code structure which is something like this

reqdValue value = ask(Actor1)
if (value != null) {
  // do something with value 
}

I want to ask how I can accomplish this in a non-blocking way, without doing a futures.get() or something similar. In the akka documentation, there is an context.ask() that can be used between actors, but it doesn’t mention if it is non-blocking or not.

https://doc.akka.io/docs/akka/current/typed/interaction-patterns.html

Also, when the response is received, the response will be received in the messageAdapter behavior, but what if you require the value in the main routine - how do we do that ?

You should look at the sending futures to self portion where you’d not block on the future, handle the possible throwable (like if the actor ref is not responding, or the ask timed out, etc.) and the expected response by forwarding a new message back to yourself for the response.
You’d still have the same behavior in the end, likely using Behaviors.same() or self if using OO actors over functional actors.

Some production code that I’ve done with this sort of interaction (albeit very not documented) that “works” is here.

1 Like

I came up with a different idea - but not sure if it is technically sound.

Instead of

reqdValue value = ask(Actor1)
if (value != null) {
  // do something with value 
}

I will simply do a tell on the Actor from whom I want a response (instead of Ask).

Let’s say I am expecting APIResponse from Actor1, I will create a message adapter for APIResponse in Actor2.

Inside Actor2, in the handler for APIResponse, I execute rest of the logic which is requires APIResponse. This is opposed to generating a CompletionStage using an ask on Actor1.

Does this make sense?

I’d recommend context.ask as in the documentation. Then you can transform the response to the message type that the asking actor understands.

Yes, it’s non-blocking.

1 Like