Exemplo n.º 1
0
        static void Main(string[] args)
        {
            string hocon       = System.IO.File.ReadAllText(@".\hocon.cfg");
            var    config      = ConfigurationFactory.ParseString(hocon);     // .WithFallback(...);
            var    actorSystem = ActorSystem.Create("ConsoleApp", config);

            ColoredConsole.WriteLineGreen($"[{Assembly.GetExecutingAssembly().FullName}]");
            ColoredConsole.WriteLineGreen($"'{actorSystem.Name}' Actor System Created.");

            // start the Application Actors
            // var consoleUi = actorSystem.ActorOf<ConsoleUiActor>("ConsoleUi");
            var consoleUi = actorSystem.ActorOf(Props.Create <ConsoleUiActor>(), "ConsoleUi");

            consoleUi.Tell("start");

            // actorSystem.Terminate();

            // wait for the actor system to terminate
            ColoredConsole.WriteLine("Awaiting for ActorSystem Termination.");
            actorSystem.WhenTerminated.Wait();
            ColoredConsole.WriteLine("ActorSystem Terminated.");

            ColoredConsole.WriteLine("Press 'enter' to exit.");
            Console.ReadLine();
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
 protected override void OnReceive(object message)
 {
     if (message is CommandCompleted)
     {
         ColoredConsole.WriteLineGreen(((CommandCompleted)message).Data);
     }
     else if (message is string)
     {
         ColoredConsole.WriteLine((string)message);
     }
     else
     {
         // call unhandled for any message you are not going to deal with.
         Unhandled(message);
     }
 }
Exemplo n.º 4
0
        private bool Handle(Shared.Messages.Command command)
        {
            // create a command handling actor instance if needed and then
            // forward the command
            var childActorId = GetHashMapping(command);
            var child        = Context.Child(childActorId);

            if (child == ActorRefs.Nobody)
            {
                child = Context.ActorOf(Props.Create <CommandHandlerWorkerActor>(_writer), childActorId);
            }
            // check for stop, poisonpill, kill, gracefulstop commands..
            // It's responsibility of this Actor to manage its children
            switch (command)
            {
            case StopCommand cmd:
                Context.Stop(child);
                return(true);

            case PoisonPillCommand cmd:
                child.Tell(PoisonPill.Instance);
                return(true);

            case KillCommand cmd:
                child.Tell(Kill.Instance);
                return(true);

            case GracefulStopCommand cmd:
                try
                {
                    var gracefulStop = child.GracefulStop(TimeSpan.FromSeconds(5));
                    gracefulStop.Wait();
                    ColoredConsole.WriteLineGreen("GracefulStop completed");
                }
                catch (AggregateException ex)
                {
                    // the GracefulStop can fail if it cannot complete within the specified TimeSpan.
                    // The Task will be cancelled.
                    ColoredConsole.WriteLineYellow($"GracefulStop failed, exception: {ex}");
                }
                return(true);
            }

            // child.Tell(command);
            child.Forward(command);
            return(true);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            var actorSystem = ActorSystem.Create("ConsoleApp");

            ColoredConsole.WriteLineGreen($"{actorSystem.Name} Actor System Created.");

            // start the Application Actors
            // var consoleUi = actorSystem.ActorOf<ConsoleUiActor>("ConsoleUi");
            var consoleUi = actorSystem.ActorOf(Props.Create <ConsoleUiActor>(), "ConsoleUi");

            consoleUi.Tell("start");

            // actorSystem.Terminate();

            // wait for the actor system to terminate
            ColoredConsole.WriteLine("Awaiting for ActorSystem Termination.");
            actorSystem.WhenTerminated.Wait();
            ColoredConsole.WriteLine("ActorSystem Terminated.");

            ColoredConsole.WriteLine("Press 'enter' to exit.");
            Console.ReadLine();
        }
Exemplo n.º 6
0
        protected override void OnReceive(object message)
        {
            var text = Console.ReadLine();

            // Parse the Command (not production code! :D)
            // First check the stop/kill commands directed to the router
            switch (text)
            {
            case QuitCommand:
                _consoleUi.Tell("terminate");
                return;

            case StopCommand:
                // Send a System Stop message to the specified actor (the message will have priority over others already in the mailbox).
                // Actor receives the Stop message and suspends the actor’s Mailbox (other messages will go to DeadLetters).
                // Actor tells all its children to Stop. Stop messages propagate down the hierarchy below the actor.
                // Actor waits for all children to stop.
                // Actor calls PostStop lifecycle hook method for resource cleanup.
                // Actor shuts down.
                Context.Stop(_commandHandler);

                ReadNext();
                return;

            case PoisonPillCommand:
                // Use a PoisonPill message if you want the actor to process its mailbox before shutting down.
                // PoisonPill is a message that will be placed in the mailbox.
                // When the Actor process the message the above mentioned stop sequence will be initiated.
                _commandHandler.Tell(PoisonPill.Instance);

                ReadNext();
                return;

            case KillCommand:
                // Use a Kill message if you want it to show in your logs that the actor was killed.
                // Send a System Kill message to the specified actor.
                // The actor throws an ActorKilledException (The actor’s supervisor logs this message).
                // This suspends the actor mailbox from processing further user messages.
                // The actor’s supervisor handles the ActorKilledException and issues a Stop directive.
                // The actor will stop following the above mentioned stop sequence.
                _commandHandler.Tell(Kill.Instance);

                ReadNext();
                return;

            case GracefulStopCommand:
                // If you want confirmation that the actor was stopped within a specified Timespan.
                // It will send a PoisonPill message and 'start a timer to check if the actor stops within the specified amount of time'.
                // It will return a Task<bool> you can wait on to know if the Actor was stopped.
                // The Task can be cancelled if the Actor does not stop with the specified TimeSpan.
                try
                {
                    var gracefulStop = _commandHandler.GracefulStop(TimeSpan.FromSeconds(5));
                    gracefulStop.Wait();
                    ColoredConsole.WriteLineGreen("GracefulStop completed");
                }
                catch (AggregateException ex)
                {
                    // the GracefulStop can fail if it cannot complete within the specified TimeSpan.
                    // The Task will be cancelled.
                    ColoredConsole.WriteLineYellow($"GracefulStop failed, exception: {ex}");
                }

                ReadNext();
                return;
            }

            // Try to parse the User Commands: 'UserId command'
            var parsedText = text.Split(' ');

            if (parsedText.Length == 2)
            {
                Command cmd;
                switch (parsedText[1])
                {
                case EscalateExceptionCommand:
                    cmd = new EscalateExceptionCommand();
                    break;

                case RestartExceptionCommand:
                    cmd = new RestartExceptionCommand();
                    break;

                case ResumeExceptionCommand:
                    cmd = new ResumeExceptionCommand();
                    break;

                case StopExceptionCommand:
                    cmd = new StopExceptionCommand();
                    break;

                case PauseCommandHandlerCommand:
                    cmd = new PauseCommandHandlerCommand();
                    break;

                case ResumeCommandHandlerCommand:
                    cmd = new ResumeCommandHandlerCommand();
                    break;

                default:
                    // Tell something!
                    cmd = new InputCommand(parsedText[1]);

                    /*
                     * // Ask something!
                     * ColoredConsole.WriteLineYellow("Awaiting for the command to complete");
                     * var completedTask = _commandHandler.Ask<InputCommandResponse>(new InputCommandRequest(parsedText[1]));
                     * completedTask.Wait(); // blocking operation: wait for completion and a specific reply
                     * ColoredConsole.WriteLineYellow($"Response: {completedTask.Result.Data}");
                     * ColoredConsole.WriteLineYellow("The command has been completed");
                     */

                    break;
                }
                cmd.Context.UserId = parsedText[0];
                _commandHandler.Tell(cmd);
            }
            else
            {
                ReportUnsupportedCommand(text);
            }
            ReadNext();

            // we have no unhandled message here, every message will activare the read cycle again.
        }
Exemplo n.º 7
0
        protected override void OnReceive(object message)
        {
            // actually every message activates the reader

            var text = System.Console.ReadLine();

            switch (text)
            {
            case QuitCommand:
                _consoleUi.Tell("terminate");
                return;

            case StopCommand:
                // Actor receives the Stop message and suspends the actor’s Mailbox (other messages will go to DeadLetters).
                // Actor tells all its children to Stop. Stop messages propagate down the hierarchy below the actor.
                // Actor waits for all children to stop.
                // Actor calls PostStop lifecycle hook method for resource cleanup.
                // Actor shuts down.
                Context.Stop(_commandHandler);
                break;

            case PoisonPillCommand:
                // Use a PoisonPill message if you want the actor to process its mailbox before shutting down.
                // PoisonPill is a message that will be placed in the mailbox.
                // When the Actor process the message the above mentioned stop sequence will be initiated.
                _commandHandler.Tell(PoisonPill.Instance);
                break;

            case KillCommand:
                // Use a Kill message if you want it to show in your logs that the actor was killed.
                // The actor throws an ActorKilledException. The actor’s supervisor logs this message.
                // This suspends the actor mailbox from processing further user messages.
                // The actor’s supervisor handles the ActorKilledException and issues a Stop directive.
                // The actor will stop following the above mentioned stop sequence.
                _commandHandler.Tell(Kill.Instance);
                break;

            case GracefulStopCommand:
                // If you want confirmation that the actor was stopped within a specified Timespan.
                // It will return a Task<bool> you can check to know if the Actor was stopped.
                var gracefulStop = _commandHandler.GracefulStop(TimeSpan.FromSeconds(5));
                gracefulStop.Wait();
                if (gracefulStop.Result)
                {
                    ColoredConsole.WriteLineGreen("GracefulStop completed");
                }
                else
                {
                    ColoredConsole.WriteLineYellow("GracefulStop failed");
                }
                break;

            case EscalateExceptionCommand:
                _commandHandler.Tell(new EscalateExceptionCommand());
                break;

            case RestartExceptionCommand:
                _commandHandler.Tell(new RestartExceptionCommand());
                break;

            case ResumeExceptionCommand:
                _commandHandler.Tell(new ResumeExceptionCommand());
                break;

            case StopExceptionCommand:
                _commandHandler.Tell(new StopExceptionCommand());
                break;

            case DumpStatusCommand:
                _commandHandler.Tell(new DumpStatusCommand());
                break;

            default:
                _commandHandler.Tell(text);
                break;
            }
            // send a message to outself stating we are ready to read the next command
            Self.Tell("readnext");

            // we have no unhandled message here, every message will activare the read cycle again.
        }