Search code examples
javaakkaactor

Is possible retrieve multiples actorRef from ActorSelection?


I have try get multiples ActorRef from ActorSelection. Someone know if is possible ?

My code

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


Solution

  • It is not possible using resolveOne, as the name suggests it returns a single reference. You could send a message to the ActorSelection which would send the message to every Actor matching the selection, and have those actors reply. If you use the built-in Identify message, then you wouldn't need to alter the receiving actors at all. From the docs at 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.