Created
February 27, 2016 07:37
-
-
Save Horusiath/5d8b3c5a5cf512610bd4 to your computer and use it in GitHub Desktop.
Akka.NET layered hierarchy with custom supervisor strategy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CustomException : Exception { } | |
| public class Boom { } | |
| public class Grandpa : ReceiveActor | |
| { | |
| public Grandpa() | |
| { | |
| var parent = Context.ActorOf(Props.Create<Parent>(), "parent"); | |
| ReceiveAny(x => parent.Forward(x)); | |
| } | |
| protected override SupervisorStrategy SupervisorStrategy() | |
| { | |
| return new OneForOneStrategy(10, 10, exception => | |
| { | |
| if (exception is CustomException) | |
| { | |
| Console.WriteLine("Custom exception received"); | |
| return Directive.Stop; | |
| } | |
| else return Akka.Actor.SupervisorStrategy.DefaultDecider(exception); | |
| }); | |
| } | |
| } | |
| public class Parent : UntypedActor | |
| { | |
| public Parent() | |
| { | |
| Context.ActorOf(Props.Create<Child>(), "child-1"); | |
| Context.ActorOf(Props.Create<Child>(), "child-2"); | |
| Context.ActorOf(Props.Create<Child>(), "child-3"); | |
| } | |
| protected override void OnReceive(object message) | |
| { | |
| if(message is Boom) throw new CustomException(); | |
| else | |
| foreach (var child in Context.GetChildren()) | |
| { | |
| child.Forward(message); | |
| } | |
| } | |
| } | |
| public class Child : UntypedActor | |
| { | |
| protected override void OnReceive(object message) | |
| { | |
| Console.WriteLine("{0} received {1}", Self, message); | |
| } | |
| protected override void PreStart() | |
| { | |
| Console.WriteLine("{0} is starting", Self); | |
| base.PreStart(); | |
| } | |
| protected override void PostStop() | |
| { | |
| Console.WriteLine("{0} is stopping", Self); | |
| base.PostStop(); | |
| } | |
| } | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| using (var system = ActorSystem.Create("sys")) | |
| { | |
| var grandpa = system.ActorOf(Props.Create<Grandpa>(), "grandpa"); | |
| grandpa.Tell("ok"); | |
| grandpa.Tell(new Boom()); | |
| Console.ReadLine(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment