Skip to content

Instantly share code, notes, and snippets.

@Horusiath Horusiath/Example.cs
Created Feb 27, 2016

Embed
What would you like to do?
Akka.NET layered hierarchy with custom supervisor strategy
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
You can’t perform that action at this time.