Esempio n. 1
0
        public void ConsumeMessages(string queue, ushort prefetchCount = 10)
        {
            var channel = _config.Channel();

            DeclareQueue(channel, queue);

            channel.BasicQos(prefetchSize: 0, prefetchCount: prefetchCount, global: false);
            var scope    = _serviceProvider.CreateScope();
            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += async(model, ea) =>
            {
                var body    = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                var @event  = this._deserializer.Deserialize(message);

                object subscriber = DomainEventSubscribers.ContainsKey(queue)
                    ? DomainEventSubscribers[queue]
                    : SubscribeFor(queue, scope);

                try
                {
                    await((IDomainEventSubscriberBase)subscriber).On(@event);
                }
                catch
                {
                    HandleConsumptionError(ea, @event, queue);
                }

                channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
            };

            string consumerId = channel.BasicConsume(queue: queue, autoAck: false, consumer: consumer);
        }
Esempio n. 2
0
        public void ConsumeMessages(string queue, ushort prefetchCount = 10)
        {
            var channel = _config.Channel();

            DeclareQueue(channel, queue);

            channel.BasicQos(0, prefetchCount, false);
            var scope    = _serviceProvider.CreateScope();
            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += async(model, ea) =>
            {
                var message = Encoding.UTF8.GetString(ea.Body.Span);
                var @event  = _deserializer.Deserialize(message);

                var subscriber = DomainEventSubscribers.ContainsKey(queue)
                    ? DomainEventSubscribers[queue]
                    : SubscribeFor(queue, scope);

                try
                {
                    await((DomainEventSubscriberBase)subscriber).On(@event);
                }
                catch
                {
                    HandleConsumptionError(ea, @event, queue);
                }

                channel.BasicAck(ea.DeliveryTag, false);
            };

            var consumerId = channel.BasicConsume(queue, false, consumer);
        }
        public void Publish(string exchangeName, string eventName, string message)
        {
            var channel = _config.Channel();

            channel.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Topic);

            var body       = Encoding.UTF8.GetBytes(message);
            var properties = channel.CreateBasicProperties();

            properties.Headers = new Dictionary <string, object>
            {
                { HeaderReDelivery, 0 }
            };

            channel.BasicPublish(exchange: exchangeName,
                                 routingKey: eventName,
                                 basicProperties: properties,
                                 body: body);
        }
Esempio n. 4
0
        public void Configure()
        {
            var channel = _config.Channel();

            var retryDomainEventExchange      = RabbitMqExchangeNameFormatter.Retry(_domainEventExchange);
            var deadLetterDomainEventExchange = RabbitMqExchangeNameFormatter.DeadLetter(_domainEventExchange);

            channel.ExchangeDeclare(_domainEventExchange, ExchangeType.Topic);
            channel.ExchangeDeclare(retryDomainEventExchange, ExchangeType.Topic);
            channel.ExchangeDeclare(deadLetterDomainEventExchange, ExchangeType.Topic);

            foreach (var subscriberInformation in _domainEventSubscribersInformation.All())
            {
                var domainEventsQueueName = RabbitMqQueueNameFormatter.Format(subscriberInformation);
                var retryQueueName        = RabbitMqQueueNameFormatter.FormatRetry(subscriberInformation);
                var deadLetterQueueName   = RabbitMqQueueNameFormatter.FormatDeadLetter(subscriberInformation);
                var subscribedEvent       = EventNameSubscribed(subscriberInformation);

                var queue = channel.QueueDeclare(queue: domainEventsQueueName,
                                                 durable: true,
                                                 exclusive: false,
                                                 autoDelete: false);

                var retryQueue = channel.QueueDeclare(queue: retryQueueName, (bool)true,
                                                      (bool)false,
                                                      (bool)false,
                                                      (IDictionary <string, object>)RetryQueueArguments(_domainEventExchange, domainEventsQueueName));

                var deadLetterQueue = channel.QueueDeclare(queue: deadLetterQueueName, durable: true,
                                                           exclusive: false,
                                                           autoDelete: false);

                channel.QueueBind(queue, _domainEventExchange, domainEventsQueueName);
                channel.QueueBind(retryQueue, retryDomainEventExchange, domainEventsQueueName);
                channel.QueueBind(deadLetterQueue, deadLetterDomainEventExchange, domainEventsQueueName);

                channel.QueueBind(queue, _domainEventExchange, subscribedEvent);
            }
        }
        public void Configure()
        {
            var channel = _config.Channel();

            var retryDomainEventExchange      = RabbitMqExchangeNameFormatter.Retry(_domainEventExchange);
            var deadLetterDomainEventExchange = RabbitMqExchangeNameFormatter.DeadLetter(_domainEventExchange);

            channel.ExchangeDeclare(_domainEventExchange, ExchangeType.Topic);
            channel.ExchangeDeclare(retryDomainEventExchange, ExchangeType.Topic);
            channel.ExchangeDeclare(deadLetterDomainEventExchange, ExchangeType.Topic);

            foreach (var subscriberInformation in _domainEventSubscribersInformation.All())
            {
                var domainEventsQueueName = RabbitMqQueueNameFormatter.Format(subscriberInformation);
                var retryQueueName        = RabbitMqQueueNameFormatter.FormatRetry(subscriberInformation);
                var deadLetterQueueName   = RabbitMqQueueNameFormatter.FormatDeadLetter(subscriberInformation);
                var subscribedEvent       = EventNameSubscribed(subscriberInformation);

                var queue = channel.QueueDeclare(domainEventsQueueName,
                                                 true,
                                                 false,
                                                 false);

                var retryQueue = channel.QueueDeclare(retryQueueName, true,
                                                      false,
                                                      false,
                                                      RetryQueueArguments(_domainEventExchange, domainEventsQueueName));

                var deadLetterQueue = channel.QueueDeclare(deadLetterQueueName, true,
                                                           false,
                                                           false);

                channel.QueueBind(queue, _domainEventExchange, domainEventsQueueName);
                channel.QueueBind(retryQueue, retryDomainEventExchange, domainEventsQueueName);
                channel.QueueBind(deadLetterQueue, deadLetterDomainEventExchange, domainEventsQueueName);

                channel.QueueBind(queue, _domainEventExchange, subscribedEvent);
            }
        }