如何使用BackoffSupervisor

时间:2018-08-02 08:23:34

标签: akka.net

  1. 版本Akka.NET v1.3.8
  2. 平台Windows 10

我试图了解BackoffSupervisor应该如何工作。这是我的测试应用程序。

class Program
    {
        static void Main(string[] args)
        {
            var config = ConfigurationFactory.ParseString(@"
                akka {  
                    stdout-loglevel = DEBUG
                    loglevel = DEBUG
                    log-config-on-start = off  
                    log-dead-letters = 10
                    actor {
                        debug {  
                              receive = on 
                              autoreceive = on
                              lifecycle = on
                        }
                    }
                }");

            using (var system = ActorSystem.Create("TestSystem", config))
            {
                var childs = Child.Props();
                var parent = BackoffSupervisor.Props(
                    Backoff.OnFailure(
                        childs,
                        "myChilds",
                        TimeSpan.FromSeconds(3),
                        TimeSpan.FromSeconds(30),
                        0.5));

                var supervisor = system.ActorOf(parent, "supervisor");                

                while (true)
                {
                    var input = Console.ReadLine();

                    if (input.ToLower() == "exit")
                        break;
                    else
                        supervisor.Tell(input);
                }

                system.Terminate().Wait();
            }
        }
    }

    public class ParentSuperVisor : ReceiveActor
    {
        public static Props Props()
        {
            return Akka.Actor.Props.Create(() => new ParentSuperVisor());
        }
    }

    public class Child : ReceiveActor
    {
        public Child()
        {
            Receive<string>(msg => HandleMessage(msg));
        }

        private void HandleMessage(string msg)
        {
            throw new Exception("Error");
        }

        public static Props Props()
        {
            return Akka.Actor.Props.Create(() => new Child()).WithRouter(new RoundRobinPool(3));
        }
    }

在此实现中,如果儿童角色之一摔倒,每个人都会摔倒并重新开始一切。我只需要重启摔倒的演员。有人可以告诉吗?

预先感谢您的回答。

0 个答案:

没有答案
相关问题