Exemplo n.º 1
0
        public void Start()
        {
            lock (this)
            {
                Console.WriteLine("Customer " + customerID + " is running. Waiting for a reply.");
            }

            OrderRequestMessage request = new OrderRequestMessage
            {
                CustomerId = customerID,
                ProductId  = productID,
                Country    = country
            };

            using (IBus bus = RabbitHutch.CreateBus("host=localhost;persistentMessages=false"))
            {
                // Listen to reply messages from the Retailer (use Topic Based Routing).
                // WRITE CODE HERE!

                // Send an order request message to the Retailer (use a point-to-point channel).
                // WRITE CODE HERE!

                // Block this thread so that the customer instance will not exit.
                Console.ReadLine();
            }
        }
        public void Start()
        {
            lock (this)
            {
                Console.WriteLine("Customer " + customerID + " is running. Waiting for a reply.");
            }

            OrderRequestMessage request = new OrderRequestMessage
            {
                CustomerId = customerID,
                ProductId  = productID,
                Country    = country
            };

            using (IBus bus = RabbitHutch.CreateBus("host=localhost;persistentMessages=false"))
            {
                // Listen to reply messages from the Retailer (use Topic Based Routing).
                bus.PubSub.Subscribe <OrderReplyMessage>(this.customerID.ToString(), HandleOrderEvent, x => x.WithTopic(this.customerID.ToString()));
                // WRITE CODE HERE!

                // Send an order request message to the Retailer (use a point-to-point channel).
                //SEND
                bus.SendReceive.Send("customerToRetailerQueue", request);
                // WRITE CODE HERE!

                // Block this thread so that the customer instance will not exit.
                Console.ReadLine();
            }
        }
Exemplo n.º 3
0
        void HandleOrderRequestMessage(OrderRequestMessage message)
        {
            lock (this)
            {
                Console.WriteLine("Warehouse " + id + ": Order request received for order "
                                  + message.OrderId + ".");
            }

            int     daysForDelivery = country == message.Country ? 2 : 10;
            decimal shippingCharge  = country == message.Country ? 5 : 10;

            Product requestedProduct =
                products.FirstOrDefault(p => p.ProductId == message.ProductId);

            if (requestedProduct != null)
            {
                OrderReplyMessage replyMessage = new OrderReplyMessage
                {
                    WarehouseId     = id,
                    OrderId         = message.OrderId,
                    ItemsInStock    = requestedProduct.ItemsInStock,
                    DaysForDelivery = daysForDelivery,
                    ShippingCharge  = shippingCharge
                };

                // Send the reply message to the Retailer.
                bus.SendReceive.Send("warehouseToRetailerQueue", replyMessage);

                lock (this)
                {
                    Console.WriteLine("Warehouse " + id + ": Reply for order " +
                                      message.OrderId + " sent back to retailer.");
                }
            }
        }
        static void Timeout_Elapsed(object message)
        {
            OrderRequestMessage m = message as OrderRequestMessage;

            lock (lockObject)
            {
                Console.WriteLine("Processing replies for request from customer " + m.CustomerId + ".");

                // Get all replies from warehouses for this specific order.
                var repliesForThisOrder = allReplies.FindAll(r => r.OrderId == m.OrderId);

                if (repliesForThisOrder.Count > 0)
                {
                    // Get a reply from a local warehouse (DaysForDelivery == 2) if possible.
                    var reply =
                        repliesForThisOrder.FirstOrDefault(r => r.DaysForDelivery == 2);
                    if (reply == null)
                    {
                        // Otherwise, accept the first reply from one of the other warehouses.
                        reply = repliesForThisOrder.First();
                    }

                    // Remove the replies for this specific order from the "allReplies" list,
                    // because they have already been processed.
                    allReplies.RemoveAll(r => r.OrderId == m.OrderId);

                    // Uses Topic Based Routing to send the reply to a customer.
                    // The topic ís the CustomerId for the outstanding request.
                    bus.PubSub.Publish <OrderReplyMessage>(reply, m.CustomerId.ToString());


                    Console.WriteLine("Order: " + reply.OrderId);
                    Console.WriteLine("Warehouse: " + reply.WarehouseId);
                    Console.WriteLine("Items: " + reply.ItemsInStock);
                    Console.WriteLine("Shipping charge: " + reply.ShippingCharge);
                    Console.WriteLine("Days for delivery: " + reply.DaysForDelivery);
                }
                else  // if there was no reply from any warehouse
                {
                    Console.WriteLine("No items in stock for this product.");
                    OrderReplyMessage replyNoItems = new OrderReplyMessage();
                    replyNoItems.OrderId = m.OrderId;
                    bus.PubSub.Publish <OrderReplyMessage>(replyNoItems, m.CustomerId.ToString());
                }
            }
        }
Exemplo n.º 5
0
        static void HandleOrderRequest(OrderRequestMessage message)
        {
            message.OrderId = ++orderId;

            // Send an order request message to all warehouses (publish-subscribe channel).
            // WRITE CODE HERE!

            lock (lockObject)
            {
                Console.WriteLine("Order request message from customer " + message.CustomerId + " published");
            }

            // Create a timer to wait for a specified amount of time for replies from
            // warehouses. When the timeout occurs, the specified event handler
            // (Timeout_Elapsed) will process the replies from warehouses and reply
            // to the customer who created the order request.
            // Beware that a timer runs in a separate thread.
            Timer timer = new Timer(Timeout_Elapsed, message, timeoutInterval, Timeout.Infinite);
        }