1

Given an Akka.net-based actor system with some basic structure like:

/user
  /coordinator
    /child (x1000, with RoundRobinPool router)

Coordinator actor defines supervision strategy with Directive.Restart used.

Child actors could fail for several reasons (for example, with ArithmeticException, InvalidOperationException and MyCustomException).

But when a child fails with MyCustomException, I'd like to have an ability to somehow additionally handle it without changing the default supervision mechanism (restart approach should still work here). For example, to add Console.Writeline with exception details.

How do I implement it?

4

1 回答 1

1

一般来说MyCustomException,当异常发生时您负责,您可以立即将其记录在您的子逻辑中,而无需将其提升为父级。但如果不可能,您可以像这样定义自己的主管策略类:

public class MySupervisorStrategy : OneForOneStrategy
{
    public MySupervisorStrategy(ILoggingAdapter log) : base(reason =>
        {
            if (reason is MyCustomException)
            {
                log.Error(reason.Message);
                return Directive.Restart;
            }

            return Akka.Actor.SupervisorStrategy.DefaultDecider.Decide(reason);
        })
    {
    }
}

有两种方法可以将它应用到你的演员:

  1. 用于Props.Create<MyActor>().WithSupervisorStrategy(new MySupervisorStrategy(system.Log)直接从您的演员系统应用它。
  2. 通过覆盖actor的SupervisorStrategy方法(用于Context.GetLogger()接收当前actor的日志实例)将其直接附加到actor的逻辑中。

第二个选项不太灵活,但在需要使用远程部署方案的情况下可能会更好。

于 2015-08-17T12:25:53.780 回答