scala 如何使用actorSelection选择akka演员?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26541784/
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 to select akka actor with actorSelection?
提问by Cherry
I am trying to select one actor which have already created. Here is a code:
我正在尝试选择一个已经创建的演员。这是一个代码:
val myActor = system.actorOf(Props(classOf[MyActor]), "myActorName")
println("myActor path - " + akka.serialization.Serialization.serializedActorPath(myActor))
println("Selection from spec akka://unit-test/user/myActorName " + system.actorSelection("akka://unit-test/user/myActorName").resolveOne().value)
println("Selection from spec /user/myActorName/ " + system.actorSelection("/user/myActorName/").resolveOne().value)
The result is:
结果是:
myActor path - akka.tcp://[email protected]:46635/user/myActorName#1444872428
Selection from spec akka://unit-test/user/myActorName None
Selection from spec /user/myActorName/ None
Also I can pass a message to actor and it completes well. What I missed during actorSelection? How to select actor properly?
我也可以将消息传递给演员并且它完成得很好。我在演员选择期间错过了什么?如何正确选择演员?
UPDATED
更新
It is very strange, but when I replace system.actorSelection("/user/myActorName/").resolveOne().valuewith system.actorFor("/user/myActorName/")everything works. I mean actorForreturns an actor. (Which is not a right solution due to actorForis deprecated)
这很奇怪,但是当我system.actorSelection("/user/myActorName/").resolveOne().value用system.actorFor("/user/myActorName/")一切替换时。我的意思是actorFor返回一个演员。(由于actorFor已弃用,这不是正确的解决方案)
回答by dk14
Please, be careful with futures. In your case you're receiving future which may be not completed at the calling moment - so its value may be empty:
请谨慎对待期货。在您的情况下,您收到的未来可能在调用时未完成 - 因此其值可能为空:
scala> println("Selection from spec /user/myActorName/ " + system.actorSelection("/user/myActorName/").resolveOne().value)
Selection from spec /user/myActorName/ None
vs
对比
scala> val fut = system.actorSelection("/user/myActorName/").resolveOne()
fut: scala.concurrent.Future[akka.actor.ActorRef] = scala.concurrent.impl.Promise$DefaultPromise@7eb8d7bd
<just wait some time here>
scala> fut.value
res21: Option[scala.util.Try[akka.actor.ActorRef]] = Some(Success(Actor[akka://default/user/myActorName#1625966960]))
To obtain value correctly use onComplete or just for-comprehesion/map:
要正确获取值,请使用 onComplete 或仅使用for-comprehesion/ map:
import scala.concurrent.ExecutionContext.Implicits.global
for (res <- system.actorSelection("/user/myActorName").resolveOne()) {
println(res)
}
Keep in mind that onComplete/for are implemented as listeners, so they might be executed in different thread. If you need the result in current thread - use classical Await.result(it's blocking - so, you should do it outside of actor's context/receive).
请记住,onComplete/for 是作为侦听器实现的,因此它们可能会在不同的线程中执行。如果您需要当前线程中的结果 - 使用经典Await.result(它是阻塞的 - 所以,您应该在 actor 的 context/ 之外执行它receive)。

