As mentioned in the tutorial Introduction to Apache Akka, Akka Actors communicate with each other using messages, which are any type of object but they must be immutable. In general, objects like String, Int, Boolean are all immutable objects.

In this tutorial, we will study three ways of sending Akka messages: Message Sending, Message Replying and Message Forwarding (Message Receiving was already explained on the tutorial Writing a basic Akka program):

* Message Sending: Akka messages can be sent by either of the following two methods:

– tell() or “!”: based on fire-forget principle (sending a message but do not wait for reply). If the message is sent from an actor, the sender will be that actor, otherwise it will be deadLetters by default.

– ask() or “?”: based on Send-And-Receive-Future (Future is the possible reply that can be received in the future. If we don’t get Future within a specified time, an AskTimeOutException error will occur. Unlike tell(), ask() doesn’t use sender(), so the sender is Actor.noSender (see more at here)

* Message Replying: we use sender() function to send a reply message to the sender. This function returns a result that is the ActorRef of the sender (reply message can be sent by either of two methods described in Message Sending)

* Message Forwarding: Akka allows user to forward message from one actor to another. In this case , the address/reference of the original sender is remained unchanged even if the message has been transmitted through several intermediate actors

Now, we will write a program that demonstrates the above three types of message sending by using ask() function to send a message from the main() function to MessageActor. After receiving the message, MessageActor send a reply message to the sender. It will also send a message to ChildActor using both forward() and tell() to compare the differences between these two methods. The full code of this program is provided below.

Run the program, we get the following result:

Because the ask() function uses Actor.noSender, the name of the sender that send message from the main() function is $a (if we continue sending other massages, the name of these messages’ sender will be $b, $c,…). In addition, if we use the forward() function to forward the message, the sender of this message is still $a. However if we uses the tell() function, the original sender is replaced by the Actor that uses tell() function (in this example it is MessageActor).

January 14, 2019
ITechSeeker