Akka Actor回复消息示例详解

【Akka Actor回复消息示例详解】你可以使用sender()方法回复邮件。此方法为你提供ActorRef, 它是发送方Actor的实例。你也可以存储此参考。回复消息很简单, 你只需要使用sender()即可!回复消息。
如果没有发送者, 即在没有参与者或将来上下文的情况下发送的消息, 则默认发送者为” 死信” 参与者参考。
Akka演员回复消息示例

import akka.actor.{Actor, ActorSystem, Props}; class ActorReplyExample extends Actor{ def receive = { case message:String => println("Message recieved from "+sender.path.name+" massage: "+message); val child = context.actorOf(Props[ActorChildReplyExample], "ActorChild"); child ! "Hello Child" } }class ActorChildReplyExample extends Actor{ def receive ={ case message:String => println("Message recieved from "+sender.path.name+" massage: "+message); println("Replying to "+sender().path.name); sender()! "I got you message"; } }object ActorReplyExample{ def main(args:Array[String]){ val actorSystem = ActorSystem("ActorSystem"); val actor = actorSystem.actorOf(Props[ActorReplyExample], "RootActor"); actor ! "Hello"; } }

输出
Message recieved from deadLetters massage: Hello Message recieved from RootActor massage: Hello Child Replying to RootActor Message recieved from ActorChild massage: I got you message

    推荐阅读