我正在研究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
}
Run Code Online (Sandbox Code Playgroud)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());
Run Code Online (Sandbox Code Playgroud)DeatchWatch:创建另一个actor调用getContext().watch(actorRef of actorToWatch); 并检查是否收到终止消息.这可能仅用于已创建的actor.
1,2讲述了演员和3名监视器的存在.我想知道这三个用例及其对演员邮箱和功能的影响,以便我可以选择适合我的应用程序的类型.
检查演员是否存在是一种好习惯?如果不是为什么?.
好吧,只有一种方法可以知道过去某个时刻是否存在Actor:如果您从中收到消息。以上所有只是这个主题的变体。
就是说,一旦有了ActorRef,就可以使用DeathWatch通知该演员终止的消息。但是,尚未收到Terminated消息并不表示演员还活着:Terminated可能已经在路上。
将Actor视为只能通过发送电子邮件进行交流的人。这种类比对于它们交互的语义非常有效。