scala akka 演员中 forward 和 tell 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25115547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between forward and tell in akka actors
提问by Micha? Jurczuk
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
回答by Konrad 'ktoso' Malawski
The sender()will be different on the receiving end.
的sender()将是在接收端的不同。
Message sends using tell(also known as !):
使用tell(也称为!)发送消息:
Atellsmessage Mto B. Btellsthat message to C. Cthinks the sender()of message Mis B.
A告诉消息M来B。将该消息B告诉C. C认为sender()消息M是B。
Message sends using forward:
使用forward发送消息:
Atellsmessage Mto B. Bforwardsthat message to C.Cthinks the sender()of message Mis A.
A告诉消息M来B。将该消息B转发到C. C认为sender()消息M是A。
Worth pointing out is, that you can achieve the same as forwardwhen explicitly setting the sender of a message using tell, however this is not typical Akka-style:
值得指出的是,您可以实现与forward使用 显式设置消息的发送者相同的效果tell,但这不是典型的 Akka 风格:
// inside `B`, when received `msg` from `A`
C tell (msg, A)
==
C forward msg
For more info refer to the docs about forward.
有关更多信息,请参阅有关 forward的文档。
回答by monkHyman
Tell sets the sender as the actor sending the message.
Tell 将发送者设置为发送消息的参与者。
Forward keeps the original sender of the message.
Forward 保留消息的原始发件人。
回答by Suresh Kumar
target.tell(message, getSelf()); final Object result = ""; target.forward(result, getContext());
target.tell(message, getSelf()); 最终对象结果 = ""; target.forward(result, getContext());
Here, getself() is the self reference of the actor. getcontext() is the supervisor reference.
这里,getself() 是actor 的自引用。getcontext() 是主管参考。
回答by Aamir
import akka.actor.{Actor, ActorSystem, Props}
case object FromActor3
/**
* forward method: Forwards the message and passes the original sender actor as the sender.
*/
object ActorForward extends App {
class ActorExample extends Actor {
def receive = {
case message: String =>
println(s"Message received from ${sender.path.name}, message = $message")
val child = context.actorOf(Props[Actor2], "ChildActor")
child ! message
case FromActor3 => println("Response when forwarded by Actor2 to Actor3")
}
}
class Actor2 extends Actor {
def receive = {
case message: String =>
println(s"Message received from ${sender.path.name}, message = $message")
val child = context.actorOf(Props[Actor3], "ChildActor")
println("forwarding...")
child forward message
case _ => println("Unknown message")
}
}
class Actor3 extends Actor {
def receive = {
case message: String =>
println(s"Message received from ${sender.path.name}, message = $message")
sender ! FromActor3
case _ => println("Unknown message")
}
}
val actorSystem = ActorSystem("ActorSystem")
val actor = actorSystem.actorOf(Props[ActorExample], "RootActor")
actor ! "Hello"
}

