Example #1
0
        static void Main()
        {
            var bus = new TopicBasedPubSub();
            var messageListener = new MessageListener(bus);
            var startables = new List<IStartable>();
            
            var midgetFactory = new MidgetFactory();
            var midgetHouse = new MidgetHouse(bus, midgetFactory);

            var consolePrinter = new QueuedHandler<OrderPaid>(Messages.Paid, new ConsolePrintingOrderHandler(bus));
            bus.Subscribe(consolePrinter);

            startables.Add(consolePrinter);
            var cashier = new Cashier(bus);
            var queuedCashier = new QueuedHandler<TakePayment>(Messages.OrderBilled, cashier);
            bus.Subscribe(queuedCashier);
            startables.Add(queuedCashier);
            startables.Add(cashier);

            var assistantManager = new QueuedHandler<PriceOrder>(Messages.OrderPrepared, new AssistantManager(bus));
            bus.Subscribe(assistantManager);
            startables.Add(assistantManager);
          
            var chefs = new List<QueuedHandler<CookFood>>();
            var rand = new Random();
            for (int i = 0; i < NumberOfChefs; i++)
            {
                var chef = new TimeToLiveDispatcher<CookFood>(new Chef(bus, rand.Next(1000)));
                var queuedHandler = new QueuedHandler<CookFood>(string.Format("Chef {0}", i), chef);
                chefs.Add(queuedHandler);
                startables.Add(queuedHandler);
            }

            var distributionStrategy = new QueuedDispatcher<CookFood>(bus, chefs);
            startables.Add(distributionStrategy);

            foreach (var startable in startables)
            {
                startable.Start();
            }
            var monitor = new Monitor(startables);
            monitor.Start();

            var waiter = new Waiter(bus);
            
            for (int i = 0; i < 10; i++)
            {
                var correlationId = Guid.NewGuid();

                var orderId = waiter.PlaceOrder(correlationId);    
            }            

            Console.ReadKey();
        }
Example #2
0
        public void Handle(OrderPlaced message)
        {
            var midget = MidgetFactory.Create(message.IsDodgyCustomer, _topicBasedPubSub);

            _midgets[message.CorellationId] = midget;
            _topicBasedPubSub.SubscribeByCorellationId <OrderPlaced>(message.CorellationId, midget);
            _topicBasedPubSub.SubscribeByCorellationId <OrderCooked>(message.CorellationId, midget);
            _topicBasedPubSub.SubscribeByCorellationId <CookingTimedOut>(message.CorellationId, midget);
            _topicBasedPubSub.SubscribeByCorellationId <OrderPriced>(message.CorellationId, midget);
            _topicBasedPubSub.SubscribeByCorellationId <OrderPaid>(message.CorellationId, midget);

            midget.Handle(message);
        }