我如何检查Akka演员是否存在(akka 2.2)? [英] How can i check if an Akka actor exists (akka 2.2)?

查看:256
本文介绍了我如何检查Akka演员是否存在(akka 2.2)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不是actor的java对象,它使用actorSelection(Path)从一个actor系统中选择actor。可能是所选的actor在系统中不存在。

I have a java object which is not an actor which selects actors from an actor system with actorSelection(Path)). It is possible, that the selected actor does not exist in the system.

在Java Api中,ActorSelection不存在ask(),因此我无法发送和标识

In the Java Api ask() does not exist for ActorSelection, so I can not send and Identify message to the actor selection and use the sender of the response.

我试图通过通过参与者选择将消息发送给参与者来解决该问题,然后做出响应死信但是我没有任何死信。

I tried to solve the problem by sending the message to the actor anyway via the actor selection and then reacting to the deadletter. But I don't get any deadletters.

我如何通过ActorSelection检查演员是否还活着?

How can I check with the ActorSelection if the actor is alive or does not exist?

ActorSystem system = ActorSystem.create("test");

//create test actor
system.actorOf(Props.create(TestActor.class), "testActor");

//add dead letter listener to the system
ActorRef eventBusActor = asys.actorOf(Props.create(EventBusActor.class), "eventbusactor");
system.eventStream().subscribe(eventBusActor, DeadLetter.class);


//This works. The test actor receives the message      
ActorSelection a1 = asys.actorSelection("/user/testActor");
a1.tell("hello", ActorRef.noSender());

//This does not work and does not send dead letters      
ActorSelection a2 = asys.actorSelection("/user/doesnotexist");
a2.tell("hello", ActorRef.noSender());

//Does not compile, because ask needs an ActorRef as first argument
ActorSelection a3 = asys.actorSelection("/user/test");
Future f = Patterns.ask(a3, new Identify(), 1000);


推荐答案

似乎Akka放弃了对<$ c的支持Java API上的$ c> ActorSelection ,用于询问。我稍微玩了一下代码,但是发现了一些可行的方法。看看下面的代码是否对您有用:

It looks like Akka left off support for ActorSelection on the java api for ask. I played with the code a little and I found something that works though. See if this code works for you:

import java.util.concurrent.TimeUnit;

import scala.concurrent.Await;
import scala.concurrent.Future;

import akka.actor.ActorIdentity;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.Identify;
import akka.actor.Props;
import akka.pattern.AskableActorSelection;
import akka.util.Timeout;

public class AskTest {

  public static void main(String[] args) throws Exception{
    ActorSystem sys = ActorSystem.apply("test");
    sys.actorOf(Props.create(TestActor.class), "mytest");

    ActorSelection sel = sys.actorSelection("/user/mytest");

    Timeout t = new Timeout(5, TimeUnit.SECONDS);
    AskableActorSelection asker = new AskableActorSelection(sel);
    Future<Object> fut = asker.ask(new Identify(1), t);
    ActorIdentity ident = (ActorIdentity)Await.result(fut, t.duration());
    ActorRef ref = ident.getRef();
    System.out.println(ref == null);
  }
}

我只是看了scala请求支持的工作方式,通过Java迷上了它。这对我有用;我希望它对您有用。

I just looked at how the scala ask support worked and hooked into it via java. This worked for me; I'm hoping it works for you.

这篇关于我如何检查Akka演员是否存在(akka 2.2)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆