我有1000个订单,我想为每个唯一的orderid创建一个actor。在确保每个唯一的id只拥有一个演员的同时,安全地创建这些角色的最佳方法是什么?
I have 1000 orders and I want to create an actor for each unique orderid. What would be the best way to safely create these actors while guaranteeing that I only have one per unique orderid?
我尝试先使用ActorSelection,然后,如果找不到具有该ID的演员,则使用ActorOf创建一个新的演员,但是当开始批量这些我会得到很多ActorNotFoundException,然后当我尝试使用ActorOf时,它会失败,并出现InvalidActorNameException。
I have tried to first use ActorSelection, then if I can't find an actor with that id, I use ActorOf to create a new one, but when starting batches of these I will get a lot of ActorNotFoundException and when I then try to use ActorOf it fails with InvalidActorNameException.
示例:
try { actorRef = await actorSelection.ResolveOne(TimeSpan.FromMilliseconds(3000)); } catch (ActorNotFoundException) { actorRef = Actor.EwmsActorSystem.ActorOf<T>(actorId); }推荐答案
您应使用每个孩子模式的实体,即,有一个actor可以根据每个实体ID生成子actor。您可以查看
You should use the Entity Per Child Pattern, i.e. have an actor that spawns child actors per entity ID. You can view an example in the World Crawler sample.
总而言之,它看起来应该像这样:
In a nutshell, it should look something like this:
var child = Context.Child(entityId.ToString()); if (child == ActorRefs.Nobody) child = Context.ActorOf(...); // spawn child actor here child.Tell(message);同样好的做法是在子演员上设置ReceiveTimeout,以在有演员时杀死他们闲置了一段时间。
It is also good practice to set a ReceiveTimeout on the child actors, to kill them off when they have been idle for a period of time.
更多推荐
解决演员的正确方法是什么
发布评论