我有一个代码,我正在检查一个演员是否已经不存在,我们将创建它,但问题是我的代码正在使用未来的 OnComplete 回调,我在函数/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
}
我正在调用这样的代码
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]
我怎样才能得到实际的 ActorRef 它给了我 null 但演员正在创建,我试图通过这样做将 ref 存储在两个块中
actorRef=result //success block
actorRef=regularAdminIndexMongoActor //failure block
我认为它在调用 onComplete 并返回 null 之前返回值,因为我在我的函数开始时初始化了变量 null 我该如何解决这个问题?请帮助我如何实现我想要的 ActorRef