gpt4 book ai didi

java - 我如何检查 Akka Actor 是否存在(akka 2.2)?

转载 作者:IT老高 更新时间:2023-10-28 20:44:13 33 4
gpt4 key购买 nike

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

在 Java Api 中,ActorSelection 不存在 ask(),因此我无法向 Actor 选择发送和识别消息并使用响应的发送者。

我试图通过 Actor 选择将消息发送给 Actor 然后对死信作出 react 来解决问题。但我没有收到任何死信。

如何检查 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);

最佳答案

我最近发现了 ActorSelection.resolveOne 方法:

val name = "myActor"
implicit val timeout = 5000 // Timeout for the resolveOne call
system.actorSelection(name).resolveOne().onComplete {
case Success(actor) => actor ! message

case Failure(ex) =>
val actor = system.actorOf(Props(classOf[ActorClass]), name)
actor ! message
}

我仍在调查的一个问题是,定义 this 的方法可能会同时调用(来自其他参与者)。因此,如果 resolveOne 调用失败,则可能会出现竞争条件,您尝试创建两次 actor,因为该 actor 仍在创建中。对于您的用例来说,这可能是也可能不是问题

关于java - 我如何检查 Akka Actor 是否存在(akka 2.2)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18012945/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com