当我使用像 And 这样的行为组合器时,akka 类型会创建演员吗?

Does akka-typed create actors when I use behavior combinator like And?

在我的系统中,我希望演员 A 向演员 B,C,D 发送相同的消息。 我没有创建这三个演员,而是想将他们的行为与 And 行为污染器结合起来,然后将该行为传递给 A.

如果我这样做,将创建多少个演员?我会只得到一个具有三种行为的演员,还是三个具有不同行为的演员?

为了具体起见,这是我使用非 And 方法的真实代码(查看 ReplyGenerator 如何将引用传递给其他参与者):

object Foobar {
  def foobar(): Behavior[Request] =
  ContextAware[Request] {
    context =>
      val foo1 = context.spawn(Props(Foo1.behavior()), "foo1")
      val foo2 = context.spawn(Props(Foo2.behavior()), "foo2")
      val foo3 = context.spawn(Props(Foo3.behavior()), "foo3")
      val generator = context.spawn(Props(ReplyGenerator.behavior(List(foo1, foo2, foo3))),
        "generator")

      Static {
        case request: Request =>
          generator ! request
      }
  }
}

这里是 ReplyGenerator 向所有订阅者发送相同消息的行为:

object ReplyGenerator {
def behavior(subscribers: List[ActorRef[Reply]]): Behavior[Request] = {
    Static {
      case request: Request =>
        subscribers.foreach(_ ! Reply.empty)
    }
 }

考虑到我希望演员 foo 1,2,3 到 运行 并行,这里可以使用 And 组合器吗?

谢谢。

如果您指的是执行并行性,那么您必须像示例代码中那样创建单独的 Actors(通过单独的 spawn 调用)——使用 And 只会创建一个单独的 Actor依次运行包含的行为。