So i have started learning Akka and trying out the examples in typesafe. So the Hello Akka app has the following code :
import akka.actor.{ ActorRef, Act
The purpose of tell
, also represented as !
is to send a message to an actor. As actors communicate via message passing, tell
is the mechanism used to support that message passing. It is asynchronous to the caller, so that once the caller calls tell
, they are decoupled from the receiving and processing of that message by the target actor instance. In this particular example, the code is using tell to cause the greeter actor to update its internal state. As this interaction does not result in a response of any kind (the receiving actor is not sending a message back to the sender in response to this request), tell
alone is sufficient here.
For getting a greeting response from the greeter, the interaction is slightly different. In this interaction, the sender is expecting a response message. This type of request/response interaction can be handed via ask
(i.e. ?
) in which the caller gets a Future
back that will be completed when the receiver replies but ask
was not used here. In this example, the coder got the request/response semantic by using an Inbox
instead which can behave as an actor and it eliminates the need for futures. The inbox must look like an ActorRef
to the receiver allowing it to route a response back to it with the following line:
sender ! Greeting(greeting)
The sender()
method returns the ActorRef
of whoever sent in the message being currently processed. The Inbox
must be able to represent itself as an ActorRef
for this to work. But as I said earlier, you also could have used ask
here to get the request/response interaction working. I think it's just a matter of choice. The bottom line is that tell
is used for the fire and forget interaction model and the inbox (or another actor or ask
) can be used when a request/response semantic is needed.
When you do the following you are dealing directly with an ActorRef
greeter.tell(WhoToGreet("akka"), ActorRef.noSender)
On the other hand, when you use an Inbox
you are dealing something that is not exactly an actor but actor-like. One scenario where this is helpful is when you don't want to create your own actor but still want to interact with other actors asynchronously. Please see this
An Inbox is an actor-like object which is interrogated from the outside. It contains an actor whose reference can be passed to other actors as usual and it can watch other actors’ lifecycle.
The actor runs on the a threadpool (configured using a dispatcher). You can decide either by configuration or by putting in your code where which dispatcher that actor use for its execution.