[英]How to get Akka actor by name as an ActorRef?
In Akka I can create an actor as follows. 在Akka我可以创建一个演员如下。
Akka.system(app).actorOf(Props(classOf[UnzipActor]), name="somename")
Then I am in a different class, how can I get this actor? 然后我在另一个班级,我怎么能得到这个演员呢?
I can get an ActorSelection
我可以得到一个ActorSelection
lazy val unzip: ActorSelection =
Akka.system.actorSelection("user/" + "somename")
However, a ActorSelection
is not what I want; 但是, ActorSelection
不是我想要的; I want an ActorRef
. 我想要一个ActorRef
。 How can I get a ActorRef
? 我如何获得ActorRef
?
I want to have an ActorRef
since I wish to schedule a call to an ActorRef
using the scheduler. 我希望有一个ActorRef
因为我希望使用调度程序调度一个ActorRef
。
Akka.system(app).scheduler.schedule(
5 seconds, 60 seconds, mustBeActorRef, MessageCaseClass())
You can use the method resolveOne
on an ActorSelection to get an ActorRef asynchronously. 您可以在resolveOne
上使用方法resolveOne
来异步获取ActorRef。
implicit val timeout = Timeout(FiniteDuration(1, TimeUnit.SECONDS))
Akka.system.actorSelection("user/" + "somename").resolveOne().onComplete {
case Success(actorRef) => // logic with the actorRef
case Failure(ex) => Logger.warn("user/" + "somename" + " does not exist")
}
ref : http://doc.akka.io/api/akka/2.3.6/index.html#akka.actor.ActorSelection 参考: http : //doc.akka.io/api/akka/2.3.6/index.html#akka.actor.ActorSelection
Looking up Actors by Concrete Path : 通过具体路径查找演员 :
To acquire an
ActorRef
that is bound to the life-cycle of a specific actor you need to send a message, such as the built-inIdentify
message, to the actor and use thesender()
reference of a reply from the actor. 要获取绑定到特定actor的生命周期的ActorRef
,您需要向actor发送消息(例如内置Identify
消息)并使用来自actor的回复的sender()
引用。
But for the case you're describing, it might be more appropriate to use the scheduler to send a message to an ActorRef
you already have (like self
or a new temporary actor), and react to that message by sending a MessageCaseClass
to actorSelection("user/somename")
. 但是对于您正在描述的情况,使用调度程序将消息发送到您已经拥有的ActorRef
(如self
或新的临时actor)可能更合适,并通过向actorSelection("user/somename")
发送MessageCaseClass
来actorSelection("user/somename")
消息做出反应actorSelection("user/somename")
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.