2017-07-25 175 views
0

我目前正在建立一个系统中有相当多的地方,其中有多种方法可以联系到同一个演员。例如,如果你有一个持久的汽车演员,你可以使用VIN或车牌。使用间接参考查找以前持续存在的Akka演员

由于我们在重新创建actor时需要一个“真实名称”作为actor名称/持久性ID,因此这些“lookups/references”本身就是actors,它们以键名命名,只保留actor的ID参考。

这似乎是正确的方法吗?好像很多演员不是真正的演员,只是代理人。

回答

0

编辑更新答案。

听起来我们有一个存储库,其中包含一系列汽车,每辆汽车都可以有VIN或REG编号(或序列号或底盘编号......唯一标识汽车的东西)。

ID | VIN | REG 
car1 | ABC | 123 
car2 | DEF | 456 

我们也有一个持续的CarActor封装的状态和逻辑一辆车。

public class CarActor : PersistentReceiveActor 
{ 
    string _id; 
    public override string PersistenceId { get { return _id; } } 

    public CarActor(string id) 
    { 
     _id = id; 
    } 

    public static Props Props(string id) 
    { 
     return Akka.Actor.Props.Create(() => new CarActor(id)); 
    }  
} 

当我们重新创建一个演员的时候需要一个“真实名称”作为演员的名字/持久使用 ID,这些“查找/引用”本身 演员,他们的主要的名字命名,坚持只有他们引用的演员 的ID。

这似乎是正确的方法吗?看起来好像很多 演员不是真正的演员,只是代理人。

为了简化事情,我们可以定义一个消息来封装汽车可以识别的各种ID号码。这条消息可以传给我们的Actor系统进行处理。

public class CarCommand 
{ 
    public string Vin { get; private set; } 
    public string Reg { get; private set; } 
} 

Best practice是有一个主管或路由器的演员,负责实体的域,并选举代表每个实体作为自己的演员。该主管可以收到一个CarCommand消息,通过VIN或REG查找汽车的ID,并找到/创建一个小孩演员处理消息。

public class CarSupervisor : ReceiveActor 
{ 
    //in reality this would be a repository e.g. a DB context... it would 
    //be even better if this was handled in another Actor that this Actor 
    //has access to 
    readonly IEnumerable<Cars> _cars; 

    public CarSupervisor(IEnumerable<Cars> cars) 
    { 
     _cars = cars; 
     Receive<CarCommand>(command => 
     { 
      //find a car by VIN or REG or other variable 
      var car = _cars.First(c => c.VIN == command.VIN); 
      //see if any child actors have been created for this car instance 
      var child = Context.Child(car.Id); 
      //if we don't have an incarnation yet, create one 
      if (Equals(child, ActorRefs.Nobody)) 
       child = Context.ActorOf(CarActor.Props(car.Id), car.Id)); 
      //tell the child to process the message 
      child.Forward(command); 
     }); 
    }  
} 
+0

感谢您的回答,但我不确定您是否理解了这个问题。这位演员没有天然的关键。我可以尝试通过VIN,注册牌等来找到它。除了每个参考(VIN,注册牌)都有独立的演员以外,是否有任何方法可以找到汽车?另外,在内存列表中将不可行。 – jameswilddev

+0

不是VIN还是注册唯一密钥?例如。一个VIN只能与一辆车相关,后者在我们的仓库中有一个ID。 'IdSupplierActor'可以接收包含VIN或Reg等的信息,并返回有问题车辆的ID。或者一个'CarFinderActor'接受这个消息并返回一个到Actor的路径。 –

+0

这就是我的意思;那么如何使用Akka Persistence来实现呢?拥有一个拥有每个VIN或注册牌号的演员是不可行的。谢谢。 – jameswilddev