private bool Handle(RoutedCommandEnvelope obj) { // todo: load the proper command handler(s) and do the job var message = obj.Command; if (message is EscalateExceptionCommand) { throw new EscalateException(); } else if (message is RestartExceptionCommand) { throw new RestartException(); } else if (message is ResumeExceptionCommand) { throw new ResumeException(); } else if (message is StopExceptionCommand) { throw new StopException(); } else { _writer.Tell($"[{Self.Path}] {message}"); } return(true); }
private bool Handle(RoutedCommandEnvelope command) { // create a command handing actor instance if needed // forward the command to them var childActorName = GenerateActorName(command); var child = Context.Child(childActorName); if (child == ActorRefs.Nobody) { switch (command.RouteTo.Type) { case ActorTypes.CommandHandler1: child = Context.ActorOf(Props.Create <CommandHandler1>(_writer), childActorName); break; case ActorTypes.CommandHandler2: child = Context.ActorOf(Props.Create <CommandHandler2>(_writer), childActorName); break; case ActorTypes.Room: child = Context.ActorOf(Context.DI().Props <RoomActor>(), childActorName); break; } } // check for stop, poisonpill, kill, gracefulstop commands.. // It's responsibility of this Actor manage its children switch (command.Command) { case StopCommand cmd: Context.Stop(child); return(true); case PoisonPill cmd: child.Tell(PoisonPill.Instance); return(true); case KillCommand cmd: child.Tell(Kill.Instance); return(true); case GracefulStopCommand cmd: var gracefulStop = child.GracefulStop(TimeSpan.FromSeconds(5)); gracefulStop.Wait(); if (gracefulStop.Result) { ColoredConsole.WriteLineGreen("GracefulStop completed"); } else { ColoredConsole.WriteLineYellow("GracefulStop failed"); } return(true); } // child.Tell(command.Command); child.Forward(command.Command); return(true); }
private static string GenerateActorName(RoutedCommandEnvelope command) { return(command.RouteTo.Type + command.RouteTo.Id); }