Created
May 8, 2015 11:49
-
-
Save Danthar/5c44f93e65706467cc46 to your computer and use it in GitHub Desktop.
Simple akka supervision and retry with IUnboundedStash example
This file contains 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
class Program | |
{ | |
static void Main(string[] args) { | |
var system = ActorSystem.Create("failboat"); | |
var actor = system.ActorOf(Props.Create(() => new ParentActor())); | |
actor.Tell(5); //this will fail | |
actor.Tell("This message will work"); | |
Console.ReadLine(); | |
} | |
} | |
class ParentActor : UntypedActor { | |
private IActorRef inline; | |
private int failed; | |
protected override void PreStart() { | |
inline = Context.ActorOf(Props.Create(() => new InlineRetryActor())); | |
} | |
protected override void OnReceive(object message) { | |
inline.Tell(message); | |
} | |
protected override SupervisorStrategy SupervisorStrategy() | |
{ | |
return new OneForOneStrategy(5, 500, (ex) => { | |
if (failed < 5) { | |
failed++; | |
return Directive.Restart; | |
} | |
failed = 0; | |
return Directive.Resume; | |
}); | |
} | |
} | |
class InlineRetryActor : UntypedActor, IWithUnboundedStash { | |
protected override void PreRestart(Exception reason, object message) | |
{ | |
//when using stashing, akka automatically unstashes when restarting | |
Stash.Stash(); //<-- if you want the message to be placed back in the front of the mailbox | |
//Self.Forward(message); //<-- if you want the message to be placed at the back of the mailbox | |
} | |
protected override void OnReceive(object message) { | |
Console.WriteLine("received inline: "+message); | |
if (message is int) { | |
throw new ArgumentException("retry error: " + message); | |
} | |
} | |
public IStash Stash { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment