【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
推荐阅读
- Akka Actor介绍和用法详解
- Akka Actor转发消息示例
- Scala特性混合用法示例
- Scala字符串介绍和用法示例
- Scala特质介绍和用法示例
- Scala多维数组
- Scala final关键字用法示例
- Scala数组介绍和用法
- Scala访问修饰符用法实例