在 Akka 中,当一条消息被发送到一个 Actor Pool 时,'self' 是否持有对 Actor 实例或整个 Pool 的引用?

In Akka, when a message is sent to an Actor Pool, does 'self' hold a reference to the Actor instance or to the entire Pool?

如果我向一个 Actor 池发送一条消息,然后该 Actor 在收到该消息后向 self 发送一条消息,会发生什么情况?在这种情况下,发送到 self 是将消息发送到池中,还是发送到发送消息的特定 Actor 实例?

这个场景的伪代码如下所示

...
val system: ActorSystem = ActorSystem()
val actorPool: ActorRef = system.actorOf(
  Props(new SomeActor(someDependency))
    .withRouter(RoundRobinPool(conf.someActorPoolSize))
actorPool ! "hello"

class SomeActor(someDependency: SomeDependency) extends Actor {
  def receive = {
    case hello @ "hello" => self ! hello + " world"
    // ^Does the above send to the entire Pool, or does this specific
    // Actor just send the message directly to itself?
    case msg @ "hello world" => println(msg)
    // ^Do I know that the same Actor insntance that sent me "hello world"
    // is also going to execute "println(msg)", or could any Actor in the
    // Pool have sent the "hello world" message?
  }
}

self总是指向演员自己的ActorRef:

def receive = {
  case hello @ "hello" =>
    self ! hello + " world" // sends "hello world" to itself
  case msg @ "hello world" =>
    println(msg)

当路由发送 "hello world"self 时,它会将那个字符串发送给自己。这个 "hello world" 字符串将在 case msg @ "hello world" => 子句中被捕获,actor 将打印消息。

Do I know that the same Actor insntance that sent me "hello world" is also going to execute "println(msg)"...

外部参与者可以向路由器发送 "hello""hello world"。然后,路由器会将消息 转发 到其中一个路由。对于确定 "hello world" 消息是来自自身还是外部参与者的路由,路由必须检查 sender 的值(例如,它可以检查 sender 是否等于self).

...or could any Actor in the Pool have sent the "hello world" message?

不清楚你在这里问什么。 Routees 通常不相互通信。

How would I send a message back to the Pool, instead of to 'self'? Would I need to pass the reference to the Actor Pool as a dependency, even though my Actor is a member of the Pool it wants to send messages to?

路由不需要了解其他路由或路由器。如果您想将消息发送回池中,请让原始发送者(即,将消息发送到 router 的参与者)执行此操作。换句话说,让你的 routee 将消息发送到 sender,然后让那个 actor 将消息发送回路由器。为了强调这一点,sender 在这种情况下 不是 router/pool,而是原始发件人(对此有一些例外,如 documentation):

client            --> router --> [routee1, routee2, etc.]
(original sender)                // sender in a routee is a reference to client, because
                                 // the router forwards the messages to the routees

您可以阅读有关路由的更多信息 here