As As
As As

Reputation: 2107

Convert ActorSelection to ActorRef

I have some distribuited actors and I need the ActorRef for these actors. So I tried this line (it is in the MemberUp code).

implicit val resolveTimeout = Timeout(5 seconds)
var act=Await.result(context.system.actorSelection(RootActorPath(member.address)/"user"/"myactor2").resolveOne(), resolveTimeout.duration)

But I always get the following exception:

[ERROR] [11/03/2015 16:01:58.530] [ClusterSystem-akka.actor.default-dispatcher-5] [akka://ClusterSystem/user/myactor1] Actor not found for: ActorSelection[Anchor(akka://ClusterSystem/), Path(/user/myactor2)] akka.actor.ActorNotFound: Actor not found for: ActorSelection[Anchor(akka://ClusterSystem/), Path(/user/myactor2)]

I tried increasing the timeout to 15 or 20 seconds, but I still get the same error. The problem is that if I keep the ActorSelection (

act=context.system.actorSelection(RootActorPath(member.address)/"user"/"myactor2");

), the line works fine and I can send messages to the other actor. Why?

Upvotes: 2

Views: 1673

Answers (1)

mingchuno
mingchuno

Reputation: 557

why not just simply

val actor = context.system.actorSelection(RootActorPath(member.address)/"user"/"myactor2")
actor ! message // you can directly send message to it!

If you are sure there is an actor exist in that path, it is okey to send message like that. As the official doc said:

actorSelection only ever looks up existing actors when messages are delivered, i.e. does not create actors, or verify existence of actors when the selection is created.

Upvotes: 0

Related Questions