我正在查看 Akka.net faulttolerance 并设置了一个简单的示例,其中 Actor1 告诉 Actor2 一条消息。Actor2 抛出异常。Actor1 有一个 SuperVisorStrategy 告诉失败的actor恢复。
我实际上预计该消息会再次传递给 Actor2。但事实并非如此。所以 Actor2 恢复并可以继续处理新消息。但是现在导致 Actor 失败的消息已经消失了。这应该如何处理?我不想丢失导致异常的消息。我希望 Actor2 再次处理该消息。
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
using (ActorSystem actorSystem = ActorSystem.Create("test"))
{
IActorRef customer = actorSystem.ActorOf(Props.Create<Actor1>(), "actor1");
customer.Tell(new Start());
Console.Read();
}
}
}
public class Actor1 : UntypedActor
{
protected override SupervisorStrategy SupervisorStrategy()
{
return new OneForOneStrategy(3, TimeSpan.FromSeconds(5), ex =>
{
if (ex is ApplicationException)
return Directive.Resume;
return Directive.Escalate;
});
}
protected override void OnReceive(object message)
{
if (message is Start)
{
IActorRef actor2Ref = Context.ActorOf<Actor2>("actor2");
Context.Watch(actor2Ref);
actor2Ref.Tell(new DoSomething());
}
else if (message is Response)
{
Console.WriteLine("Response received");
return;
}
else if (message is Terminated)
{
Console.WriteLine("Terminated");
}
}
}
public class Actor2 : UntypedActor
{
protected override void OnReceive(object message)
{
if (message is DoSomething)
{
// only called once.
throw new ApplicationException("testexception");
}
}
}
public class Start
{
}
public class DoSomething
{
}
public class Response
{
}
}