在 Java Akka 中的兄弟姐妹之间发送消息
Sending messages between siblings in Java Akka
在 Java 中,使用 Akka,我有一个 Main class,它创建了两个演员(ActorA 和 ActorB)。我可以从 Main 向 Actors 发送消息,但是如何在两个 Actor 之间发送消息。
Main.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());
}
}
您可能已经猜到我是新手,因此非常感谢收到任何有用的链接。谢谢
假设演员都需要了解彼此,您有几个选项spring要考虑:
- 通过将消息从 Main 传递给每个 actor 和对方的
ActorRef
,将每个 actor 的 ActorRef
注入另一个
- 给
ActorA
和 ActorB
明确的名字,并使用他们的 ActorPath
s 向其他演员发送消息
选项 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);
}
}
您还需要 ActorA
和 ActorB
实现来处理此类消息,例如
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
如果您在创作时明确命名您的演员,那么您将能够在 ActorPath
using ActorSelection
中更可靠地称呼他们
您的主要 class 将变为:
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 来称呼其他演员,例如
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 确实存在。如果您需要,请参阅以下 link 以了解如何实现它的详细信息:http://doc.akka.io/docs/akka/2.4.2/java/untyped-actors.html#Identifying_Actors_via_Actor_Selection
在 Java 中,使用 Akka,我有一个 Main class,它创建了两个演员(ActorA 和 ActorB)。我可以从 Main 向 Actors 发送消息,但是如何在两个 Actor 之间发送消息。
Main.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());
}
}
您可能已经猜到我是新手,因此非常感谢收到任何有用的链接。谢谢
假设演员都需要了解彼此,您有几个选项spring要考虑:
- 通过将消息从 Main 传递给每个 actor 和对方的
ActorRef
,将每个 actor 的 - 给
ActorA
和ActorB
明确的名字,并使用他们的ActorPath
s 向其他演员发送消息
ActorRef
注入另一个
选项 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);
}
}
您还需要 ActorA
和 ActorB
实现来处理此类消息,例如
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
如果您在创作时明确命名您的演员,那么您将能够在 ActorPath
using ActorSelection
您的主要 class 将变为:
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 来称呼其他演员,例如
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 确实存在。如果您需要,请参阅以下 link 以了解如何实现它的详细信息:http://doc.akka.io/docs/akka/2.4.2/java/untyped-actors.html#Identifying_Actors_via_Actor_Selection