我有一个Java对象,它不是Actor,但可以使用actorSelection(Path)从actor系统中选择Actor。有可能选定的Actor在系统中不存在。
在Java API中,ActorSelection没有ask()方法,因此我不能向ActorSelection发送Identify消息并使用响应的发送者。
我尝试通过ActorSelection向Actor发送消息并通过deadletter做出反应来解决问题。但我没有收到任何deadletters。
如何使用ActorSelection检查Actor是否存活或不存在?
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);