Monday, 5 December 2016

Ask Message Pattern

Tell sends a message to an actor. Any replies to sender() are sent back to the sending actor.

Ask sends a message to an actor, and get a response back via a future. When the actor replies, it completes the future. No messages are sent to the sender's mailbox.

When you ask an Actor, Akka actually creates a temporary actor in the actor system. The sender() reference that the actor replies to becomes this temporary actor. Asks always require a timeout be defined and if the ask is not replied to, then the future will fail with the timeout.

Overhead:

Ask causes Akka to create a new temporary actor in the /temp path. This actor awaits the response from the actor that receives the Ask message. And also the overhead of the future, if your ask operations occur with extremely high frequency.

If you're invoking Tell from outside of an Actor in plain objects, there is no immediately obvious way to receive and handle a response apart from Ask. Between actors, you can handle a response by capturing some state specific to the current message in an actor.

No comments:

Post a Comment