【发布时间】:2014-04-28 03:16:01
【问题描述】:
我正在研究 akka 演员 (JAVA),最近知道有 3 种方法(可能更多)来了解演员的存在。
-
发送识别信息:
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: 创建另一个演员调用 getContext().watch(ActorRef of actorToWatch); 并检查 Terminated 消息的接收。这只能用于已经创建的actor。
1,2 告诉存在 actor 和 3 个监视器。我想知道这三个的用例以及它们对参与者邮箱和功能的影响,以便我可以选择适合我的应用程序的类型。
检查演员的存在是一种好习惯吗?如果不是,为什么? .
【问题讨论】:
-
一般来说,需要知道演员是否存在是 Akka 代码库中的代码异味。
-
在您的第一个示例中,变量
timeOut指的是什么? -
timeOut 是等待响应的时间(Timeout timeOut = new Timeout(5, TimeUnit.SECONDS))