获取之前生成的 EventSourcedBehavior 的 ActorRef。

huangapple go评论77阅读模式
英文:

Get ActorRef to previously spawned EventSourcedBehavior

问题

我们正在使用Akka Persistence中的事件源处理(event sourcing),通过扩展EventSourcedBehavior来实现。当我们创建持久性actor时,我们会给它一个唯一的名称,使用一个uuid(与我们在create内部用于构建PersistenceId的uuid相同,用于实体分片):

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

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

稍后,当我们想要向actor发送更多命令时,我们希望能从上下文中获得一个ActorRef<Command>,因为spawn返回的actorRef对象引用将不再在范围内。将命令视为后续HTTP请求的结果。

我们不能使用context.getChild(name),因为它会返回ActorRef<Void>

我们还考虑过使用Receptionist进行actor发现,但文档表示它在不同数量的actor上不可扩展:

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

另一方面,在类型化(typed)中不支持ActorSelection,详见以下链接:

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

我们对正确的方法不太确定。非常感谢您的帮助。

英文:

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&lt;Command&gt; 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&lt;Command&gt; 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&lt;Void&gt;.

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.

答案1

得分: 1

如果我理解您的问题正确的话,您想要访问之前生成的演员(Actor)的 ActorRef。以下是我通常的做法。

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;
}

另外,当演员(Actor)终止时,您还必须从映射中删除引用。

instanceIdToActor.remove(instanceId);
英文:

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&lt;String, ActorRef&lt;Command&gt; instanceIdToActor = new HashMap&lt;&gt;();

private ActorRef&lt;Command&gt; getActorRef(String instanceId) {
  ActorRef&lt;Command&gt; 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);

答案2

得分: 0

我最终在文档中找到了。在类型化系统中,处理持久化 actor 的正确方法是使用 EntityRefClusterSharding,就像下面链接中的示例一样:
https://doc.akka.io/docs/akka/current/typed/cluster-sharding.html#persistence-example

英文:

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

huangapple
  • 本文由 发表于 2020年4月10日 22:23:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/61142328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定