无法获得Supervisor策略在Akka.NET中工作

时间:2016-02-27 00:51:08

标签: akka.net supervisorstrategy

我正在努力让主管战略发挥作用。以下是该场景的说明。

A diagram of a demo app the implements a SupversiorStrategy

我有一个演员,FloorTrader,在OnReceive(对象消息)上创建一个FederalRegulator Actor:

var regulator = Context.ActorOf(Props.Create(() => new FederalRegulator("EAST_USA",trade)), "EastFedRegulator");

然后,在构建时,FederalRegulator创建StateRegulators

public class FederalRegulator : RegulatorBase
{
    public FederalRegulator(string name, Trade trade) : base(name, trade)
    {

        StateRegulate(trade);
    }

    protected override void OnReceive(object message)
    {
        var msg = message as string;

        if(!string.IsNullOrEmpty(msg) && msg.ToUpper().Equals("STOP"))throw new TradeException("No Trading Today");
    }

    private static void StateRegulate(Trade trade)
    {
       Context.ActorOf(Props.Create(() => new StateRegulator("NY", trade)), "NYRegulator");
       Context.ActorOf(Props.Create(() => new StateRegulator("MA", trade)), "MARegulator");
       Context.ActorOf(Props.Create(() => new StateRegulator("CT", trade)), "CTRegulator");
    }
}

所有Regulators在构造时都会发出Console.Write()行为,如下所示:

public abstract class RegulatorBase : UntypedActor
{
    protected RegulatorBase(string name, Trade trade)
    {
        Name = name;
        Trade = trade;
        Regulate(Name, Trade);
    }

    public string Name { get; private set; }
    public Trade Trade { get; private set; }

    protected void Regulate(string name, Trade trade)
    { // Create a timer
        var myTimer = new System.Timers.Timer();
        // Tell the timer what to do when it elapses
        myTimer.Elapsed += delegate { Console.WriteLine("{0} is regulating the trade for, {1} ", Name,Trade.Ticker); };
        // Set it to go off every 1/2 second,
        myTimer.Interval = 500;
        // And start it        
        myTimer.Enabled = true;

    }

    protected override void OnReceive(object message)
    {
       //put stuff in later
     }
}

FloorTrader演员中的SupervisionStrategy()的实现是:

protected override SupervisorStrategy SupervisorStrategy()
{
    return new OneForOneStrategy(
        0, // maxNumberOfRetries
        TimeSpan.FromSeconds(1), // duration
        x =>
          {
            if (x is TradeException)
              {
                  Console.WriteLine("---BLOW UP-----");
                  return Directive.Stop;
              }
            return Directive.Restart;
            });
}

当FederalRegulator收到STOP消息时,它会触发一个自定义异常,即TradeException,如上面的FederalRegulator代码所示。

在发出STOP消息之前的输出是预期的:

EAST_USA is regulating the trade for, HP
MA is regulating the trade for, HP
NY is regulating the trade for, HP
CT is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
MA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
MA is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
MA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
MA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
MA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
EAST_USA is regulating the trade for, HP

我的想法是,因为我正在使用OneForOneStrategy,一旦我触发STOP消息,FederalRegulator演员,发射的那个,EAST_USA is regulating the trade for, HP应该停止,但它的子节点,StateRegulators应该继续。

但是,当我使用:regulator.Tell("STOP");触发STOP消息时 抛出TradeException,但FederalRegulator不断发出。另外,我收到了死信信息:

EAST_USA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
MA is regulating the trade for, HP
---BLOW UP-----
[ERROR][2/27/2016 12:18:49 AM][Thread 0011][akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator] No Trading Today
Cause: AkkaNetDemo.Exceptions.TradeException: No Trading Today
   at AkkaNetDemo.Regulators.FederalRegulator.OnReceive(Object message) in c:\Users\Bob\Documents\GitHub\AkkaNetDemo\AkkaNetDemo\Regulators\FederalRegulator.cs:line 20
   at Akka.Actor.UntypedActor.Receive(Object message)
   at Akka.Actor.ActorBase.AroundReceive(Receive receive, Object message)
   at Akka.Actor.ActorCell.ReceiveMessage(Object message)
   at Akka.Actor.ActorCell.Invoke(Envelope envelope)
[INFO][2/27/2016 12:18:49 AM][Thread 0013][akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator] Message DeathWatchNotification from akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator to akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator was not delivered. 1 dead letters encountered.
[INFO][2/27/2016 12:18:49 AM][Thread 0012][akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator] Message DeathWatchNotification from akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator to akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator was not delivered. 2 dead letters encountered.
[INFO][2/27/2016 12:18:49 AM][Thread 0011][akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator] Message DeathWatchNotification from akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator to akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator was not delivered. 3 dead letters encountered.
EAST_USA is regulating the trade for, HP
CT is regulating the trade for, HP
MA is regulating the trade for, HP
NY is regulating the trade for, HP
Enter Trade: EAST_USA is regulating the trade for, HP
CT is regulating the trade for, HP
MA is regulating the trade for, HP
NY is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
MA is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
MA is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
MA is regulating the trade for, HP
EAST_USA is regulating the trade for, HP

任何人都可以帮助我弄清楚我的方式的错误。根据我一直在阅读的内容,当一个人使用OneForOneStrategy()时,父母应该停止,孩子们将继续。

提前致谢!!!

1 个答案:

答案 0 :(得分:2)

事实上,你已经收到了死信中的信息,这意味着该演员已被正确停止。

但是我认为,这里的主要问题可能是由于你正在使用Timer类 - 这是一个可支配的资源 - 而没有处理它并取消固定委托,这可能会导致发出控制台即使在一个演员死了之后也会写。

一般情况下,您可能不想使用计时器,因为Akka.NET为您提供了免费的scheduler功能,该功能支持以延迟或可重复的方式发送消息或执行操作。它也支持取消。

此外:

  

我的想法是因为我正在使用OneForOneStrategy,一旦我发出STOP消息,联邦调控器行为者,一个发射器,EAST_USA正在调整交易,惠普应该停止,但它的孩子,StateRegulators应该继续

OneForOneStrategy的情况下,结果指令将应用于actor,但这意味着它也会对其子项产生影响。如果演员将被停止,它的孩子也将是,因为他们不能没有父母。一对一和一对一的区别在于一对一规则是水平工作的(如果演员失败,它将应用于所有兄弟姐妹)。

相关问题