スーパーバイザー戦略を機能させようとしています。これがシナリオの図です。
SupversiorStrategy を実装するデモ アプリの図
OnReceive(オブジェクト メッセージ) で FederalRegulator アクターを作成するアクター FloorTrader があります。
var regulator = Context.ActorOf(Props.Create(() => new FederalRegulator("EAST_USA",trade)), "EastFedRegulator");
次に、構築時に、FederalRegulator は StateRegulator を作成します。
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");
}
}
すべてのレギュレーターは、次のように構築時に 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 メッセージを受信すると、上記の FederalRegulator コードに示されているように、カスタム例外 TradeException を発生させます。
発火前の 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 は続行する必要があります。
ただし、次を使用して STOP メッセージをregulator.Tell("STOP");
発行すると、TradeException がスローされますが、FederalRegulator は発行し続けます。さらに、Dead Letter メッセージが表示されます。
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()
と、親は停止し、子は続行する必要があります。