简体   繁体   English

如何用actorSelection选择akka actor?

[英]How to select akka actor with actorSelection?

I am trying to select one actor which have already created. 我试图选择一个已经创建的actor。 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://unit-test@127.0.0.1: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? 我在actorSelection期间错过了什么? How to select actor properly? 如何正确选择演员?

UPDATED 更新

It is very strange, but when I replace system.actorSelection("/user/myActorName/").resolveOne().value with system.actorFor("/user/myActorName/") everything works. 这很奇怪,但当我用system.actorSelection("/user/myActorName/").resolveOne().value替换system.actorSelection("/user/myActorName/").resolveOne().value system.actorFor("/user/myActorName/")一切正常。 I mean actorFor returns an actor. 我的意思是actorFor返回一个演员。 (Which is not a right solution due to actorFor is deprecated) (由于不支持actorFor这不是一个正确的解决方案)

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 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. 请记住,onComplete / for是作为侦听器实现的,因此它们可能在不同的线程中执行。 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 ). 如果你需要当前线程的结果 - 使用经典的Await.result (它是阻塞的 - 所以,你应该在actor的context / receive之外做)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM