gpt4 book ai didi

java - 在 Java Akka 中的 sibling 之间发送消息

转载 作者:行者123 更新时间:2023-11-30 10:46:56 25 4
gpt4 key购买 nike

在 Java 中,使用 Akka,我有一个创建两个 Actor (ActorA 和 ActorB)的 Main 类。我可以从 Main 向 Actors 发送消息,但如何在两个 Actor 之间发送消息。

主要.java:

public class Main {

public static void main(String[] args) {

// Create the ‘actorTest’ actor system.
final ActorSystem system = ActorSystem.create("actorTest");

// Create the actors.
ActorRef ActorRefA = system.actorOf(Props.create(ActorA.class));
ActorRef ActorRefB = system.actorOf(Props.create(ActorB.class));

// Send a message to ActorA.
ActorRefA.tell("message A", ActorRefA);

}

}

ActorA.java:

public class ActorA extends UntypedActor {

@Override
public void onReceive(Object message) throws Exception {
System.out.println("ActorA received: " + message);

// This is the line which I can't get to work:
ActorRefB.tell("message B", self());

}

}

正如您可能猜到的那样,我是新手,因此非常感谢收到任何有用的链接。谢谢。

最佳答案

假设 Actor 都需要了解彼此,您会想到几个选项:

  1. 通过将来自 Main 的消息与对方的 ActorRef 传递给每个 actor,将每个 actor 的 ActorRef 注入(inject)到另一个
  2. ActorAActorB 指定显式名称,并使用他们的 ActorPath 向其他参与者发送消息

选项 1

在 main 中创建 ActorA 和 ActorB 后,将每个的 ActorRef 发送给另一个,例如

public class Main {

public static void main(String[] args) {

// Create the ‘actorTest’ actor system.
final ActorSystem system = ActorSystem.create("actorTest");

// Create the actors.
ActorRef ActorRefA = system.actorOf(Props.create(ActorA.class));
ActorRef ActorRefB = system.actorOf(Props.create(ActorB.class));

// Inject ActorRefs
ActorRefA.tell(ActorRefB, ActorRef.noSender());
ActorRefB.tell(ActorRefA, ActorRef.noSender());

// Send a message to ActorA.
ActorRefA.tell("message A", ActorRefA);

}

}

您还需要您的 ActorAActorB 实现来处理这种类型的消息,例如

public class ActorA extends UntypedActor {

private ActorRef actorRefB;

@Override
public void onReceive(Object message) throws Exception {
System.out.println("ActorA received: " + message);

if (message instanceof ActorRef) {
actorRefB = message;
} else if (message instanceof String) {
// This is the line which I can't get to work:
actorRefB.tell("message B", self());
}

}

}

您显然希望对变量和事件顺序进行更复杂的检查以防止错误,但希望您能掌握要点。

选项 2

如果您在创建时明确命名您的 Actor ,那么您将能够在他们的 ActorPath 中更可靠地称呼他们使用 ActorSelection

您的主类将变为:

public class Main {

public static void main(String[] args) {

// Create the ‘actorTest’ actor system.
final ActorSystem system = ActorSystem.create("actorTest");

// Create the actors.
ActorRef ActorRefA = system.actorOf(Props.create(ActorA.class), "actorA");
ActorRef ActorRefB = system.actorOf(Props.create(ActorB.class), "actorB");

// Send a message to ActorA.
ActorRefA.tell("message A", ActorRefA);

}

}

然后在您的 ActorA 中,您将使用 Actor Selection 来称呼其他 Actor ,例如

public class ActorA extends UntypedActor {

@Override
public void onReceive(Object message) throws Exception {
System.out.println("ActorA received: " + message);

ActorSelection actorB = getContext().actorSelection("../actorB");
actorB.tell("message B", self());

}

}

应该注意的是,在使用 Actor Selection 时,不能保证您要寻址的 actor 确实存在。如果您需要,请参阅以下链接以了解如何实现它的详细信息:http://doc.akka.io/docs/akka/2.4.2/java/untyped-actors.html#Identifying_Actors_via_Actor_Selection

关于java - 在 Java Akka 中的 sibling 之间发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36307789/

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