Пример #1
0
        public void SendOrderRegisteredEvent(IOrderRegistered command)
        {
            channel.ExchangeDeclare(
                exchange: RabbitMqConstants.GetOrderRegisteredExchange(),
                type: ExchangeType.Fanout);

            var serializedCommand = JsonConvert.SerializeObject(command);

            var messageProperties = channel.CreateBasicProperties();

            messageProperties.ContentType = RabbitMqConstants.JsonMimeType;

            channel.BasicPublish(
                exchange: RabbitMqConstants.GetOrderRegisteredExchange(),
                routingKey: "",
                basicProperties: messageProperties,
                body: Encoding.UTF8.GetBytes(serializedCommand));
        }
        public void ListenForOrderRegisteredEvent()
        {
            #region queue and qos setup

            channel.ExchangeDeclare(
                exchange: RabbitMqConstants.GetOrderRegisteredExchange(),
                type: ExchangeType.Fanout);

            channel.QueueDeclare(
                queue: RabbitMqConstants.GetOrderRegisteredQueue("Notification"),
                durable: false, exclusive: false,
                autoDelete: false, arguments: null);

            channel.QueueBind(
                queue: RabbitMqConstants.GetOrderRegisteredQueue("Notification"),
                exchange: RabbitMqConstants.GetOrderRegisteredExchange(),
                routingKey: "");

            channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
            #endregion
            var eventingConsumer = new EventingBasicConsumer(channel);
            eventingConsumer.Received += (chan, eventArgs) =>
            {
                var contentType = eventArgs.BasicProperties.ContentType;
                if (contentType != RabbitMqConstants.JsonMimeType)
                {
                    throw new ArgumentException(
                              $"Can't handle content type {contentType}");
                }

                var message       = Encoding.UTF8.GetString(eventArgs.Body);
                var orderConsumer = new OrderRegisteredConsumer();
                var commandObj    =
                    JsonConvert.DeserializeObject <OrderRegistered>(message);
                orderConsumer.Consume(commandObj);
                channel.BasicAck(deliveryTag: eventArgs.DeliveryTag,
                                 multiple: false);
            };
            channel.BasicConsume(
                queue: RabbitMqConstants.GetOrderRegisteredQueue("Notification"),
                noAck: false,
                consumer: eventingConsumer);
        }