向前和告诉阿卡演员之间的区别

告诉和转发有什么区别,如果我会发送相同的消息:

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

 case msg: Message => otherActor forward msg 

sender()在接收端会有所不同。

消息使用tell (也称为! )发送:

A 告诉消息MB
B这个消息告诉 C
C认为消息Msender()B

消息使用正向发送:

A 告诉消息MB
B将该消息转发C
C认为消息Msender()A

值得指出的是,使用tell明确设置消息的发送者时,您可以实现与forward一样的function,但这不是典型的Akka风格:

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

欲了解更多信息,请参阅关于转发的文档 。

Tell将发送者设置为发送消息的演员。

转发保留消息的原始发件人。

target.tell(message,getSelf()); final Object result =“”; target.forward(result,getContext());

在这里,getself()是演员的自我引用。 getcontext()是主pipe参考。

 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" }