scala 如何从演员本身中获取 Akka 演员的姓名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19887419/
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
How can I get the name of an Akka actor from within the actor itself?
提问by Dante Romero
So, if I have an actor, I can give it a name. But, can I access that name internally? Example:
所以,如果我有一个演员,我可以给它一个名字。但是,我可以在内部访问该名称吗?例子:
class Actorz extends Actor with ActorLogging {
val actorName = //??What function
def receive = {
case x => log.debug(actorName+": Received Message: "+x)
}
}
val actor = system.actorOf(Props[Actorz], "named")
actor ! "dogs"
Now, I can pass its name as a constructor parameter. But, that seems like unnecessary duplication if there is a way to get the name internally... as it was set when I instantiated the actor using system.actorOf. API docs didn't seem to have anything.
现在,我可以将其名称作为构造函数参数传递。但是,如果有办法在内部获取名称,这似乎是不必要的重复......因为它是在我使用system.actorOf. API 文档似乎没有任何内容。
回答by Chris Martin
From an Actoryou can use selfto get the ActorRef.
从一个Actor你可以self用来获取ActorRef.
val actorName = self.path.name
http://doc.akka.io/api/akka/2.2.3/#akka.actor.Actor
http://doc.akka.io/api/akka/2.2.3/#akka.actor.Actor
http://doc.akka.io/api/akka/2.2.3/#akka.actor.ActorRef
http://doc.akka.io/api/akka/2.2.3/#akka.actor.ActorRef

