如何从演员本身中获取 Akka 演员的姓名? [英] How can I get the name of an Akka actor from within the actor itself?
问题描述
所以,如果我有演员,我可以给它起个名字.但是,我可以在内部访问该名称吗?示例:
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"
现在,我可以将其名称作为构造函数参数传递.但是,如果有办法在内部获取名称,这似乎是不必要的重复......因为它是在我使用 system.actorOf
实例化 actor 时设置的.API 文档似乎没有任何内容.
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.
推荐答案
从 Actor
您可以使用 self
来获取 ActorRef
.
From an Actor
you can use self
to get the 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.ActorRef
http://doc.akka.io/api/akka/2.2.3/#akka.actor.ActorPath
这篇关于如何从演员本身中获取 Akka 演员的姓名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!