【问题标题】:how to know if an actor exists in an actor system or not如何知道一个actor是否存在于一个actor系统中
【发布时间】:2015-12-29 12:16:51
【问题描述】:

嗨我想在演员系统中创建一个演员,如果它还没有在这里创建是我的代码

val sel = actorSystem.actorSelection("akka://ActorSystem/user/ReadOnlyAdminIndexMongoActor");
val asker = new AskableActorSelection(sel);

val future = asker.ask( Identify(1),Timeout(30 seconds))
val identity=Await.result(future, timeout.duration).asInstanceOf[ActorIdentity]         

val reference = identity.getRef
if(reference != null){
     log.info("actor does not exists")
   }
   else
   {
     log.info("actor exists"+sel.toString())
   }

但是这段代码抛出异常

17:00:19.547 1822010 [ArteciateActorSystem-akka.actor.default-dispatcher-7] EmptyLocalActorRef INFO - Message [scala.Tuple2] from Actor[akka://ActorSystem/temp/$e] to Actor[akka://ActorSystem/user/ReadOnlyAdminIndexMongoActor] was not delivered. [5] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
java.util.concurrent.TimeoutException: Futures timed out after [30 seconds]
    at scala.concurrent.impl.Promise$DefaultPromise.ready(Promise.scala:219)
    at scala.concurrent.impl.Promise$DefaultPromise.result(Promise.scala:223)
    at scala.concurrent.Await$$anonfun$result$1.apply(package.scala:190)
    at scala.concurrent.BlockContext$DefaultBlockContext$.blockOn(BlockContext.scala:53)
    at scala.concurrent.Await$.result(package.scala:190)
    at models.Global$.checkActor(Global.scala:71)

这是第 71 行的代码

val identity=Await.result(future, timeout.duration).asInstanceOf[ActorIdentity]   

请帮助我在哪里弄错了,而且我正在使用这个Link和这个Link the code givein in an accepted answer的代码

【问题讨论】:

    标签: java scala akka actor


    【解决方案1】:

    使用Identify 是确定演员是否存在的正确方法。收到询问超时通常表明它没有收到,因为那里没有参与者可以响应。我说一般是因为消息流量负载和/或远程处理可能会因其他原因导致超时。

    我的问题是,既然你有一个众所周知的地址,那么这个演员在什么情况下不存在?您只是想按需懒惰地创建它,还是试图从演员的崩溃中恢复?

    对于这两种情况,我建议使用代理模式,即有一个简单的、防崩溃的(由于其简单性)演员住在众所周知的地址,它负责按需创建 ReadOnlyAdminIndexMongoActor 和代理消息给它。这个代理也将是主管,在崩溃时恢复演员。它甚至允许您在有意义的情况下让ReadOnlyAdminIndexMongoActor 空闲超时,因为每个人都将始终通过代理与其交谈。

    这个代理的简单实现(没有主管或空闲超时处理)可能如下所示:

    class OnDemandProxyActor(proxyProps: Props) extends Actor {
    
      def receive = waiting()
    
      def waiting: Receive = {
        case msg =>
           val target = context.actorOf(proxyProps)
           target forward msg
           context.become(proxying(target))
      }
    
      def proxying(target: ActorRef): Receive = {
        case msg => target forward msg
      }
    }
    

    其中proxyPropsProps 用于按需创建您的actor,waiting 是未收到消息的初始状态,因此代理target 尚未创建,proxyingtarget 创建后的状态。两个州都使用forward 将收到的消息发送到target,就好像它来自原始sender

    【讨论】:

    • 是的,我想按需创建它,你能给出一个代理模式的代码示例吗?
    • 如果上面的例子解释了,请告诉我
    猜你喜欢
    • 1970-01-01
    • 2018-07-17
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 2020-08-16
    • 2014-10-03
    • 1970-01-01
    相关资源
    最近更新 更多