我正在使用akka actors(JAVA),最近了解到有3种(可能更多)了解actor是否存在的方法。
发送Identify消息:
ActorSelection sel = actorSystem.actorSelection("akka://test/user/TestActor"); AskableActorSelection asker = new AskableActorSelection(sel); Future<Object> future = asker.ask(new Identify(1), new Timeout(5, TimeUnit.SECONDS)); ActorIdentity identity = (ActorIdentity) Await.result(future, timeOut.duration()); ActorRef reference = identity.getRef(); if(reference != null){ // Actor exists } else { // Actor does not exits }
resolveOne方法:
ActorSelection sel = actorSystem.actorSelection("akka://test/user/TestActor"); Future<ActorRef> future = sel.resolveOne(new Timeout(5, TimeUnit.SECONDS)); // Wait for the completion of task to be completed. future.onComplete(new OnComplete<ActorRef>() { @Override public void onComplete(Throwable excp, ActorRef child) throws Throwable { // ActorNotFound will be the Throwable if actor not exists if (excp != null) { // Actor does not exists } else { // Actor exits } } }, actorSystem.dispatcher());
DeatchWatch: 创建另一个actor,调用getContext().watch(ActorRef of actorToWatch);并检查是否接收了Terminated消息。这只能用于已经创建的actor。
第1和2点告诉我们演员的存在,第3点监控。我想知道这三个的使用情况以及它们对演员邮件箱和功能的影响,以便我可以选择适合我的应用程序的类型。
检查演员是否存在是一个好习惯吗?如果不是,为什么?。
timeOut
是指什么? - KJ50