问题描述
我正在研究akka演员(JAVA),最近才知道有3种(可能更多)知道演员的存在的方式。
I am working on akka actors(JAVA) and recently came to know that there are 3 ways(may be more) to know existing of an actor.
-
发送识别消息:
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(actorToWatch的ActorRef); ,并检查是否收到已终止消息。
DeatchWatch: Create another actor call getContext().watch(ActorRef of actorToWatch); and check for receive of Terminated message. This may be used only on already created actor.
1,2告诉参与者有actor和3个监视器。我想知道这三个的用例及其对参与者邮箱和功能的影响,以便我可以选择适合我的应用程序的类型。
1,2 tells existence of actor and 3 monitors. I would like to know the use cases of these three and their effects on actors mail boxes and functionalities, so that i can choose the type which will be apt for my application.
检查演员的存在是一种好习惯吗?如果不是为什么?。
推荐答案
那么,只有一种方法可以知道某个演员是否存在指向过去:如果您收到来自它的消息。
Well, there is only one way to know whether an Actor existed at a certain point in the past: if you receive a message from it. All of the above are just variations on this theme.
也就是说,一旦有了ActorRef,就可以使用DeathWatch来通知该演员的离职。但是尚未收到终止的消息并不表示该演员还活着:终止可能已经在进行中。
That said, once you have the ActorRef, you can use DeathWatch to be notified about that actor’s termination. But not yet having received the Terminated message does not mean that the actor is still alive: the Terminated might already be on its way.
演员作为唯一的人通过发送电子邮件进行交流。这种类比对于它们的交互语义非常有效。
Think of Actors as people who can only communicate by sending email. That analogy works quite well for the semantics of their interaction.
这篇关于了解Akka演员存在的三种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!