解决演员的正确方法是什么 [英] What is the correct way to resolve an actor

查看:118
本文介绍了解决演员的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于解决演员的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆