franDayz
franDayz

Reputation: 943

Get ActorRef to previously spawned EventSourcedBehavior

We are using event sourcing with Akka Persistence by extending EventSourcedBehavior. When we create the persistent actor we give it an unique name, by using an uuid (the same we use inside create to build the PersistenceId, for entity sharding):

UUID uuid = UUID.randomUUID();
String name = MyBehavior.nameFor(uuid);

ActorRef<Command> actorRef =
    context.spawn(MyBehavior.create(uuid), name);

Later on, when we want to send further commands to the actor, we would like to get an ActorRef<Command> from the context, since the actorRef object reference returned by spawn won't be in scope anymore. Think about commands as a result of subsequents HTTP requests.

We can't use context.getChild(name) as it returns ActorRef<Void>.

We've also considered actor discovery with Receptionist, but the documentation says it doesn't scale to any number of actors:

https://doc.akka.io/docs/akka/current/typed/actor-discovery.html#receptionist-scalability

On the other hand, ActorSelection is not supported in typed, as per the following link:

https://doc.akka.io/docs/akka/current/typed/from-classic.html#actorselection

We are not sure about the right approach here. Any help would be much appreciated.

Upvotes: 0

Views: 443

Answers (2)

franDayz
franDayz

Reputation: 943

I finally found it on the documentation. In a typed system, the correct way to handle persistent actors is by using EntityRef with ClusterSharding, as in the example linked below: https://doc.akka.io/docs/akka/current/typed/cluster-sharding.html#persistence-example

Upvotes: 0

us2018
us2018

Reputation: 643

If I understand your question correctly, you want to access the ActorRef of your previously spawned actor. Here is what I usually do.

private final Map<String, ActorRef<Command> instanceIdToActor = new HashMap<>();

private ActorRef<Command> getActorRef(String instanceId) {
  ActorRef<Command> instanceActor = instanceIdToActor.get(instanceId);
  if (instanceActor == null) {
    instanceActor = getContext().spawn(MyBehavior.create(), instanceId);
    instanceIdToActor.put(instanceId, instanceActor);
  }

  return instanceActor;
}

You must also remove the reference whenever the actor dies.

instanceIdToActor.remove(instanceId);

Upvotes: 1

Related Questions