【问题标题】:creating multiple actors at once一次创建多个actor
【发布时间】:2018-04-17 15:36:02
【问题描述】:

我正在使用 AKKA 框架及其 Java API 来创建演员系统。这是演员的要点。参与者负责处理图中的节点。对于图中同一级别的节点,可以并行进行处理,因此当达到这样的级别时我需要并行生成actors

SupervisorActor extends Actor  {
// if (msg instanceOf something)
// spawn child actor for every level in a graph
childActor.tell(node, getself());

How do I send messages to two childActors here when I have two nodes at the same level?

}

【问题讨论】:

  • Actors 是异步通信的,所以你可以创建 2 个 Actor 并调用 tell 两次给他们每个人发送一条消息,他们会同时处理它。
  • 用伪代码解释一下好吗?
  • 好的,我在下面回复了答案

标签: java graph parallel-processing akka


【解决方案1】:

Actors 是异步通信的,因此您可以创建 2 个 Actor 并调用 tell 两次来向它们中的每一个发送一条消息,它们将同时处理它。

如果您满足以下条件:

ActorRef child1 = getContext().actorOf(Props.create(MyActor.class), "child1");
ActorRef child2 = getContext().actorOf(Props.create(MyActor.class), "child2");
child1.tell(msg1, getself());
child2.tell(msg2, getself());

actor child1 和 child2 将同时创建并处理消息。

【讨论】:

    【解决方案2】:

    假设您要求制作同一演员的多个实例。

    你可以在你的配置文件中定义你想要多少个actor实例。

    akka {
     /my-service {
      router = round-robin-pool // strategy how message will be served
      nr-of-instances = 3} // here you can define the number of instance
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 1970-01-01
      相关资源
      最近更新 更多