blue-sky
blue-sky

Reputation: 53786

Reuse an existing actor?

Once an actor is created :

final ActorRef greeter = getContext().actorOf(
        Props.create(MyActor.class , "myactor"));

Can this same actor be re-referenced ? Reason I'm asking I'm attempting to store some data in an actor that can be referenced by other actors.

What occurs when "greeter" is re-invoked :

final ActorRef greeter = getContext().actorOf(
        Props.create(MyActor.class , "myactor"));

Is the "greeter" actor re-instantiated ?

Upvotes: 1

Views: 588

Answers (1)

johanandren
johanandren

Reputation: 11479

If you call actorOf multiple times with the same class, what you get is multiple instances of that actor, so they will not share state. If you try to create multiple actors with the same path, you get an error - as ende said, only one actor can live on the same path.

You can look an actor up from its path. Read more about actor paths in the akka docs: http://doc.akka.io/docs/akka/2.3.6/general/addressing.html

Or, maybe a better alternative - less brittle, pass the actorRef around. The ActorRefs are immutable so they are safe to send as messages, so if you create the actor inside another you can define a protocol for asking for a reference to it from its parent.

Upvotes: 3

Related Questions