static void SendMessage(Messages.Message <Messages.Out.Order> message, string forward) { using (IModel channel = connection.CreateModel()) { channel.ExchangeDeclare(exchange: routerExchangeName, type: ExchangeType.Direct); byte[] body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)); channel.BasicPublish(exchange: routerExchangeName, routingKey: forward, basicProperties: null, body: body); Console.WriteLine($"Order {message.Body.OrderId} sent: {message.Header.Date}"); } }
private static void ConsumerReceived(object sender, BasicDeliverEventArgs args) { string message = Encoding.UTF8.GetString(args.Body); Messages.Message <Messages.In.Response> response = JsonConvert.DeserializeObject <Messages.Message <Messages.In.Response> >(message); if (response.Header.Success == false) { Console.WriteLine($"Order {response.Body.OrderId} from customer {response.Body.CustomerId} fails: Message: {response.Body.Message}"); } else { Console.WriteLine($"Order {response.Body.OrderId} from customer {response.Body.CustomerId} succeeded"); } }
private static void CreateOrderAndSend() { bool continueSending = true; int orderId = 0; while (continueSending == true) { int customerId = orderId % 2 == 0 ? 1 : 2; List <string> routingList = new List <string>(); double amount = 0; if (orderId % 2 == 0) { routingList.Add("C"); amount = 1000; } else if (orderId % 3 == 0) { routingList.Add("B"); routingList.Add("C"); amount = 500; } else { routingList.Add("B"); amount = 300; } Messages.RoutingSlip routing = new Messages.RoutingSlip(routingList); Messages.Header header = new Messages.Header(routing, true, DateTime.Now); Messages.Out.Order order = new Messages.Out.Order(orderId, customerId, amount); Messages.Message <Messages.Out.Order> message = new Messages.Message <Messages.Out.Order>(header, order); SendMessage(message, "A"); Thread.Sleep(5000); orderId++; } }