Beispiel #1
0
        static async Task MainAsync(string[] args)
        {
            var container = new Container();

            container.RegisterSingleton <IWorkProcessor, WorkProcessor>();

            var argsParsed = CommandLine.Parser.Default.ParseArguments <Options>(args);
            var ops        = argsParsed.MapResult(a => a, _ => throw new InvalidOperationException());

            Console.WriteLine($"Agents set to {ops.Agents}");

            ActorSystemInstance = ActorSystem.Create("TheActorSystem");
            var system = ActorSystemInstance;

            // Create the dependency resolver
            IDependencyResolver resolver = new SimpleInjectorDependencyResolver(container, system);

            var workItems = Enumerable.Range(0, ops.Tasks)
                            .Select(i => i % 2 == 0 ?
                                    new NumbersWorkDefinition(i + 1, ops.SleepingTimeIsMs) :
                                    new DoubleWorkDefinition(i + 1, ops.SleepingTimeIsMs))
                            .ToList();

            var workPool = system.ActorOf(Props.Create <WorkPool>(workItems));

            var router      = system.ActorOf(resolver.Create <Agent>().WithRouter(new BroadcastPool(ops.Agents)), "some-pool");
            var coordinator = system.ActorOf(Props.Create <Coordinator>(ops.Agents, router, workPool));

            coordinator.Tell(new Coordinator.GoMsg());

            // This blocks the current thread from exiting until MyActorSystem is shut down
            // The ConsoleReaderActor will shut down the ActorSystem once it receives an
            // "exit" command from the user
            await ActorSystemInstance.WhenTerminated;
        }