Akka.Net和内存中的持久性 [英] Akka.Net and In memory peristence

查看:103
本文介绍了Akka.Net和内存中的持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力保持持久性,但仍无法恢复演员.

I am trying to get my head around persistence and I am yet to be able to recover an actor.

我的意图是通过其persistenceId获取Actor(以同样的方式,我们在DDD中使用GetById获得Entity).

My intention is to get an Actor by its persistenceId (Same way we we get an Entity using GetById in DDD).

我可以获取对List的引用并将其添加到List Manager中的变量中,但我要寻找的是Actor死后如何获取具有当前状态的Actor(按事件恢复),以便可以进行修改完成.

I can get the reference to List and add it to a variable in List Manager but what I am looking for is that once the Actor dies how to get the Actor with its current state (Revovery By Events) so that modification can be done.

让我知道我的问题是否不清楚

Let me know if my question is not clear

这是我到目前为止所做的: **命令和事件**

This is what I have done so far: **Commands and Events **

  using System;
    namespace AkkaPersistence
    {
       public class CreateNewList
        {
            public string ListName { get; private set; }
            public Guid UserId { get; private set; }
           public string ListId { get; set; }

           public CreateNewList(string listName, Guid userId, string listId)
            {
                ListName = listName;
                UserId = userId;
               ListId = listId;
            }
        }


        public class RemoveList
        {
            public string ListId { get; private set; }
            public Guid UserId { get; private set; }

            public RemoveList(string listId, Guid userId)
            {
                ListId = listId;
                UserId = userId;
            }
        }


        public class ListCreated
        {
            public string ListName { get; private set; }
            public Guid UserId { get; private set; }
            public string ListId { get; private set; }

            public ListCreated(string listName, Guid userId, string listId)
            {
                ListName = listName;
                UserId = userId;
                ListId = listId;
            }
        }


        public class ListRemoved
        {
            public Guid UserId { get; private set; }
            public string ListId { get; private set; }

            public ListRemoved(Guid userId, string listId)
            {

                UserId = userId;
                ListId = listId;
            }
        }
    }

**列表类别**

   using System;
using Akka.Actor;
using Akka.Persistence;
namespace AkkaPersistence
{
   public class List: ReceivePersistentActor
   {
       public override string PersistenceId => "AKKANETLIST";

        private string Name {  get;  set; }

        private Guid CreatedBy { get; set; }

        private Guid ModifiedBy { get; set; }

        public List()
        {


            Recover<ListCreated>(evnt =>
            {
                Console.WriteLine(" List :: Recovery Hit'");
                Console.WriteLine("PID:{0}, Name {1}, CreatedBy:{2}, ModifiedBy{3}", PersistenceId, evnt.ListName, evnt.UserId, evnt.UserId);
                Name = evnt.ListName;
                CreatedBy = evnt.UserId;
                ModifiedBy = evnt.UserId;
            });




           Command<CreateNewList>(cmd =>
           {
               Console.WriteLine(" List :: Received Command 'CreateNewList'");
               var listCreated= new ListCreated(cmd.ListName,cmd.UserId, PersistenceId);

               Persist(listCreated, lc =>
               {
                   Console.WriteLine(" List::Event 'ListCreated' persisted");
                   Name = cmd.ListName;
                   CreatedBy = cmd.UserId;
                   ModifiedBy = cmd.UserId;
                  Sender.Tell(listCreated, ActorRefs.Nobody);
                   Console.WriteLine(" List::Event 'ListCreated' sent out");
               });

           });


            Command<RemoveList>(cmd =>
            {
                Console.WriteLine(" List :: Received Command 'RemoveList'");

                Console.WriteLine("PID:{0}, Name {1}, CreatedBy:{2}, ModifiedBy{3}",PersistenceId, Name, CreatedBy, ModifiedBy);
                var listRemoved = new ListRemoved(cmd.UserId,PersistenceId);

                Persist(listRemoved, lc =>
                {
                    Console.WriteLine(" List::Event 'ListRemoved' persisted");
                    ModifiedBy = cmd.UserId;
                    Sender.Tell(listRemoved, ActorRefs.Nobody);
                    Console.WriteLine(" List::Event 'ListRemoved' sent out");
                });

            });
        }
    }
}

**列表管理​​器**

** List Manager **

using System;
using Akka.Actor;

namespace AkkaPersistence
{
    public class ListManager : ReceiveActor
    {

        public ListManager()
        {
            Receive<CreateNewList>(cmd =>
            {
                Console.WriteLine(" List Manager:: Received Command 'CreateNewList'");
                var newListRef = Context.ActorOf(Props.Create(typeof(List)));
                newListRef.Tell(cmd, Self);
                Console.WriteLine(" List Manager:: Command To Create New List sent to List Actor");
            });

            Receive<RemoveList>(cmd =>
            {
                Console.WriteLine(" List Manager:: Received Command 'RemoveList'");
                var newListRef = Context.ActorOf(Props.Create(() => new List()), "AKKANETLIST");
                newListRef.Tell(cmd, Self);
                Console.WriteLine(" List Manager:: Command To 'Remove List'  sent to List Actor");
            });


            Receive<ListCreated>(evnt =>
            {
                Console.WriteLine(" List Manager:: Event 'ListCreated' Received");
            });
        }
    }
}

** Program.cs ** 命名空间AkkaPersistence { 班级计划 { 静态void Main(string [] args) {

** Program.cs ** namespace AkkaPersistence { class Program { static void Main(string[] args) {

        var system = ActorSystem.Create("MySystem");
        var listManager = system.ActorOf<ListManager>("ListManager");


        // create command
        var newListId = Guid.NewGuid().ToString("N");
        var createCommand= new CreateNewList("Akka List 1", Guid.NewGuid(), newListId);
        listManager.Tell(createCommand);



        //remove Command

        var removeCommand = new RemoveList(newListId, createCommand.UserId);
        listManager.Tell(removeCommand);

        Console.ReadLine();


    }
}

}

**控制台文本**

** Console Text **

[WARNING][1/21/2017 3:11:47 PM][Thread 0009][ActorSystem(MySystem)] NewtonSoftJsonSerializer has been detected as a default serializer. It will be obsoleted in Akka.NET starting from version 1.5 in the favor of Wire (for more info visit: http://getakka.net/docs/Serialization#how-to-setup-wire-as-default-serializer ). If you want to suppress this message set HOCON `akka.suppress-json-serializer-warning` config flag to on.
 List Manager:: Received Command 'CreateNewList'
 List Manager:: Command To Create New List sent to List Actor
 List Manager:: Received Command 'RemoveList'
 List Manager:: Command To 'Remove List'  sent to List Actor
 List :: Received Command 'CreateNewList'
 List :: Received Command 'RemoveList'
PID:AKKANETLIST, Name , CreatedBy:00000000-0000-0000-0000-000000000000, ModifiedBy00000000-0000-0000-0000-000000000000
 List::Event 'ListCreated' persisted
 List::Event 'ListCreated' sent out
 List Manager:: Event 'ListCreated' Received
 List::Event 'ListRemoved' persisted
 List::Event 'ListRemoved' sent out

**更新1 ** 2017-01-24

** Update 1 **2017-01-24

进一步的努力,我能够获得基于Name的Actor实例. 为此,我需要创建PersistenceId作为命令的一部分, 用持久性ID命名演员

further effort, I was able to get instance of an Actor based on Name. For this I need to create the PersistenceId as part of command and Name the Actor with the persistence Id

这样做,我可以使用Context.Child(Name)来获得Actor.

By doing this I could use Context.Child(Name) to get the Actor.

我正在ListManager中执行Context.Stop(newListRef):接收,假设这将停止List Actor,并在我使用Context.Child(Name)访问时强制其恢复,但这不会发生,但以某种方式出现在List中状态是正确的.不确定如何.

I am doing a Context.Stop(newListRef) in the ListManager:Receive assuming this will stop the List Actor and force it to Recover when I access using Context.Child(Name) but that does not happen, but somehow the List State is correct. Not sure how.

根据今天我收到的Horusiath的评论,我将做更多测试

I will do some more test based on comment that I received today from Horusiath

推荐答案

我可能不太理解您的问题,但是当您需要重新创建持久性actor时,只需创建具有相同PersistenceId的另一个actor实例.

I may not quite understand your question, but when you need to recreate a persistent actor, you simply create another actor instance with the same PersistenceId.

这里唯一要记住的是,您当时永远不应有多个具有相同PersistenceId的持久性参与者的活生生的实例.否则,您可能会以多个参与者同时尝试编写事件为结尾,从而可能破坏事件流.

Only thing to keep in mind here is that you should never have more than one living instance of persistent actor with the same PersistenceId at the time. Otherwise you may end with multiple actors trying to write events at the same time, possibly corrupting an event stream.

这篇关于Akka.Net和内存中的持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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