解决演员的正确方法是什么

What is the correct way to resolve an actor

我有 1000 个订单,我想为每个唯一的订单 ID 创建一个演员。什么是安全创建这些演员同时保证每个唯一 orderid 只有一个演员的最佳方式?

我尝试先使用 ActorSelection,然后如果找不到具有该 ID 的 actor,我会使用 ActorOf 创建一个新的,但是当开始批量处理时,我会得到很多 ActorNotFoundException 并且当然后我尝试使用 ActorOf,它因 InvalidActorNameException 而失败。

示例:

try
{
    actorRef = await actorSelection.ResolveOne(TimeSpan.FromMilliseconds(3000));
}
catch (ActorNotFoundException)
{
    actorRef = Actor.EwmsActorSystem.ActorOf<T>(actorId);
}

您应该使用 World Crawler 示例中的 Entity Per Child Pattern, i.e. have an actor that spawns child actors per entity ID. You can view an example

简而言之,它应该看起来像这样:

var child = Context.Child(entityId.ToString());

if (child == ActorRefs.Nobody)
    child = Context.ActorOf(...); // spawn child actor here

child.Tell(message);

在子 actors 上设置 ReceiveTimeout 也是一个好习惯,当它们闲置一段时间后将它们杀死。