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

编程入门 行业动态 更新时间:2024-10-24 18:26:14
本文介绍了解决演员的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有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.

更多推荐

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

本文发布于:2023-07-17 02:42:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128554.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:演员   正确   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!