Difference between forward and tell in akka actors
Solution 1
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.
Solution 2
Tell sets the sender as the actor sending the message.
Forward keeps the original sender of the message.
Solution 3
target.tell(message, getSelf()); final Object result = ""; target.forward(result, getContext());
Here, getself() is the self reference of the actor. getcontext() is the supervisor reference.
Michał Jurczuk
Updated on June 07, 2022Comments
-
Michał Jurczuk about 2 years
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