Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between forward and tell in akka actors

Tags:

scala

akka

actor

What is a difference between tell and forward, in case I will send the same message:

case msg: Message =>   otherActor tell (msg,sender) 

and

case msg: Message =>   otherActor forward msg 
like image 511
Michał Jurczuk Avatar asked Aug 04 '14 09:08

Michał Jurczuk


People also ask

How do actors communicate in Akka?

Actors communicate using asynchronous messages. This ensures that the sender does not stick around waiting for their message to be processed by the recipient. Instead, the sender puts the message in the recipient's mailbox and is free to do other work.

What is ActorSystem in Akka?

Companion object ActorSystem An actor system is a hierarchical group of actors which share common configuration, e.g. dispatchers, deployments, remote capabilities and addresses. It is also the entry point for creating or looking up actors. There are several possibilities for creating actors (see akka.

Are Akka actors single threaded?

Behind the scenes Akka will run sets of actors on sets of real threads, where typically many actors share one thread, and subsequent invocations of one actor may end up being processed on different threads.


1 Answers

The sender() will be different on the receiving end.


Message sends using tell (also known as !):

A tells message M to B.
B tells that message to C.
C thinks the sender() of message M is B.


Message sends using forward:

A tells message M to B.
B forwards that message to C.
C thinks the sender() of message M is A.



Worth pointing out is, that you can achieve the same as forward when explicitly setting the sender of a message using tell, however this is not typical Akka-style:

// inside `B`, when received `msg` from `A` C tell (msg, A)        ==  C forward msg 



For more info refer to the docs about forward.

like image 82
Konrad 'ktoso' Malawski Avatar answered Sep 22 '22 09:09

Konrad 'ktoso' Malawski