简体   繁体   English

如何从akka future onComplete回调的Success和Failures块中获取值

[英]how to get values from akka future onComplete callbacks 's Success and Failures blocks

i have a code in which i am checking if an actor does not exists already we will create it but the problem is my code is using future OnComplete call backs and i am doing this in an function/def and i just want to return the ActorRef here is my code 我有一个代码,我在其中检查是否不存在一个actor,我们将创建它,但问题是我的代码正在使用将来的OnComplete回调,而我在function / def中这样做,我只想返回ActorRef这是我的代码

def getRegularAdminIndexMongoActor():ActorRef= {
        var actorRef:ActorRef=null
    val sel = actorSystem.actorSelection("akka://ActorSystem/user/RegularAdminIndexMongoActor");
     val future:Future[ActorRef] = sel.resolveOne().mapTo[ActorRef] 
     future.onComplete { 
          case Success(result)=>  
          if(result != null){
            log.info("actor exists" + result)
          }
          actorRef=result
          actorRef
         case Failure(e)=>
           log.warn("in failure block actor does not exists" + e)
           val regularAdminIndexMongoActor=system.actorOf(Props[RegularAdminIndexMongoActor],name = "RegularAdminIndexMongoActor")
           log.info("created a new one "+regularAdminIndexMongoActor.path.toString())
           actorRef=regularAdminIndexMongoActor      
     }
log.info("whats is in actorRef " + actorRef)
     actorRef
         }

and i am calling the code like this 我正在这样调用代码

log.info("getting ref"+getRegularAdminIndexMongoActor)

and the output is 
15:33:39.082 555049 [play-internal-execution-context-1] Global$ INFO - whats in actorRef null
15:33:39.082 555049 [play-internal-execution-context-1] Global$ INFO - getting ref null
15:33:39.083 555050 [play-internal-execution-context-1] play INFO - Application started (Dev)
15:33:39.151 555118 [ForkJoinPool-4-worker-7] Global$ INFO - actor exists Actor[akka://ActorSystem/user/RegularAdminIndexMongoActor#-1022921773]

how can i get the actual ActorRef its giving me null but actor is creating and i tried to store the ref in both blocks by doing this 我怎样才能得到实际的ActorRef却给我null,但actor正在创建,我试图通过这样做将引用存储在两个块中

actorRef=result //success block
actorRef=regularAdminIndexMongoActor //failure block

i think its returning the values before calling onComplete and returning null as i initialized the variable null in the start of the my function how can i fix this ? 我认为它在调用onComplete之前返回值,并在我的函数开始时初始化变量null时返回null,我该如何解决呢? please help me how can i achieve my desired ActorRef 请帮助我如何实现所需的ActorRef

actorRef is a var that will get assigned not until sel.resolveOne() finishes, which could be before you are returning the value. actorRef是一个变量,在sel.resolveOne()完成之前(这可能是在您返回值之前),该变量不会被分配。 If you really want to do what you are doing in that very way you can use 如果您真的想以这种方式做自己的事,可以使用

import scala.concurrent._
import scala.concurrent.duration._

Await.result(f,Duration(6000,"millis"))

Await result will block until the future delivers or 6 seconds pass. 等待结果将一直阻塞,直到将来交付或经过6秒为止。

now, like others have stated this is not a good idea. 现在,就像其他人所说的那样,这不是一个好主意。

since you seem to be creating that very actor you can access the children directly 因为您似乎是在创造那个演员,所以您可以直接访问孩子

val child = child(name)
child.getOrElse(getContext().actorOf(..., name))

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

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