有没有办法知道是否指的是akka中不存在的演员

时间:2013-09-16 06:38:10

标签: java akka

我正在尝试将消息发布给一些现有的演员,如下面的节目,但是可能有机会在发布消息之前引用非现有演员并且我想知道。 提前致谢

actor = getContext().actorFor("actorSystem/user/" + nameOfActor);
actor.tell("message",getSelf());

4 个答案:

答案 0 :(得分:2)

您可以在发送实际消息之前向他们发送识别消息。所有演员都理解它,并应回复Self()。或者使用resolveOne方法:

  

您可以使用。获取ActorSelection的ActorRef   ActorSelection的resolveOne方法。它返回了一个未来   匹配ActorRef,如果这样的actor存在。它完成了   失败[[akka.actor.ActorNotFound]]如果没有这样的演员或者   识别未在提供的超时内完成。

答案 1 :(得分:1)

了解演员活着的唯一可能方式(没有DeathWatch)是通过接收来自它的消息。而这只能证明演员在某个时间点(当它发出信息时)还活着。

答案 2 :(得分:0)

实际上我需要处理收到的每条消息,如果演员不存在那么该消息必须单独处理,我使用Deadletter实现

final ActorRef actor = actorSystem.actorOf(new Props(DeadLetterHandlerActor.class));
actorSystem.eventStream().subscribe(actor, DeadLetter.class);

答案 3 :(得分:0)

以下是我的代码片段,我如何实现使用DeadLettersHandler actor来处理DeadLetters

public class MyActor extends UntypedActor
{
 @Override
  public void onReceive(Object message) throws Exception 
  {
     System.out.println("MyActor received : "+message.toString());
  }
}

public class DeadLettersHandler extends UntypedActor
{
  public void onReceive(Object deadLetter) throws Exception
  {
   System.out.println("DeadLettersHandler received : "+deadLetter.toString());
  }
}


public class DeadLetterTest 
{
   public static void main(String[] args)
   {
     ActorSystem MyActorSystem = ActorSystem.create("MyActorSystem");
     ActorRef existingActor = MyActorSystem.actorOf(Props.create(MyActor.class),"ExistingActor");
     ActorRef DLH = MyActorSystem.actorOf(Props.create(DeadLettersHandler.class), "DeadLetterHandler");
     MyActorSystem.eventStream().subscribe(DLH, DeadLetter.class);
     ActorSelection nonExist = MyActorSystem.actorSelection("akka://user/MyActorSystem/NonExistingActor");
     existingActor.tell("Hello Akka", existingActor);
     nonExist.tell("Hello Akka", DLH);
     MyActorSystem.shutdown();
    }
}

 output:

 MyActor received : Hello Akka
 DeadLettersHandler received : DeadLetter(Hello Akka,Actor[akka://MyActorSystem/user/DeadLetterHandler#-3707992],Actor[akka://MyActorSystem/deadLetters])
 [INFO] [10/10/2013 15:43:43.343] [MyActorSystem-akka.actor.default-dispatcher-6] [akka://MyActorSystem/deadLetters] Message [java.lang.String] from Actor[akka://MyActorSystem/user/DeadLetterHandler#-3707992] to Actor[akka://MyActorSystem/deadLetters] was not delivered. [1] 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'.