繁体   English   中英

在 Java Akka 中的兄弟姐妹之间发送消息

[英]Sending messages between siblings in Java Akka

在 Java 中,使用 Akka,我有一个 Main 类,它创建了两个演员(ActorA 和 ActorB)。 我可以从 Main 向 Actor 发送消息,但是如何在两个 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);

     }

}

演员A.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());

    }

}

正如您可能已经猜到我是新手,因此将不胜感激地收到任何有用的链接。 谢谢。

假设演员都需要了解对方,你会想到几个选项:

  1. 注入ActorRef每个演员到其他的由主传递信息到每个与其他的ActorRef
  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

如果您在创建时明确命名您的演员,那么您将能够使用ActorSelection在他们的ActorPath更可靠地寻址他们

您的 Main 类将变为:

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 选择来解决其他演员的问题,例如

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM