是否可以从 ActorSelection 中检索多个 actorRef?

Is possible retrieve multiples actorRef from ActorSelection?

我尝试从 ActorSelection 获取多个 ActorRef。有人知道是否可能吗?

我的代码

ActorRef actorRef = Await.result(getContext().actorSelection("/user/regions/*").resolveOne(timeout), timeout.duration());

不可能使用 resolveOne,顾名思义,它 returns 是一个单一的引用。您可以向 ActorSelection 发送消息,后者会将消息发送给匹配选择的每个 Actor,并让这些 Actor 回复。如果您使用内置的 Identify 消息,那么您根本不需要更改接收角色。来自 http://doc.akka.io/docs/akka/snapshot/scala/actors.html#Identifying_Actors_via_Actor_Selection:

的文档

Messages can be sent via the ActorSelection and the path of the ActorSelection is looked up when delivering each message. If the selection does not match any actors the message will be dropped.

To acquire an ActorRef for an ActorSelection you need to send a message to the selection and use the sender() reference of the reply from the actor. There is a built-in Identify message that all Actors will understand and automatically reply to with a ActorIdentity message containing the ActorRef. This message is handled specially by the actors which are traversed in the sense that if a concrete name lookup fails (i.e. a non-wildcard path element does not correspond to a live actor) then a negative result is generated. Please note that this does not mean that delivery of that reply is guaranteed, it still is a normal message.