public IExchangeOptions StartConsume <T>(Func <T, bool> factory, Func <byte[], T> msgBinder = null) where T : BaseMessage
        {
            _channel = _connection.CreateModel();

            var options = RabbitMQExchangeOptions.Build <T>();

            //Devemos realizar as associações da queue/exchange
            _channel.CreateModels(options, true);

            //Verificar se existe env configurando o limite de mensagens
            //if (qos > 0)
            //    _channel.BasicQos(0, qos, false); //Limtiar por consumer

            _logger.LogInformation("Waiting for messages.");
            var consumer = new EventingBasicConsumer(_channel);

            consumer.Received += (m, ea) =>
            {
                T message;

                if (msgBinder != null)
                {
                    message = msgBinder(ea.Body);
                }
                else
                {
                    message = DefaultMsgBinder <T>(ea.Body);
                }

                _logger.LogInformation($"New message, id {message.MessageId}");

                try
                {
                    var result = factory(message);

                    if (result)
                    {
                        _channel.BasicAck(ea.DeliveryTag, false); //Devemos indicar que a mensagem foi processado com sucesso.
                    }
                    else
                    {
                        //Devemos enviar a mensagem para a fila novamente, assim pode ser processado por outra instância desse consumer
                        _channel.BasicReject(ea.DeliveryTag, true);
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Erro ao tentar processar mensagem");
                    //Devemos enviar a mensagem para a fila novamente, assim pode ser processado por outra instância desse consumer
                    _channel.BasicReject(ea.DeliveryTag, true);
                }
            };

            _channel.BasicConsume(queue: options.QueueName,
                                  autoAck: false,
                                  consumer: consumer);

            return(options);
        }
Beispiel #2
0
 public static void CreateExchange(this IModel channel, RabbitMQExchangeOptions options)
 {
     channel.ExchangeDeclare(exchange: options.ExchangeName,
                             type: options.ExchangeType,
                             durable: options.Durable,
                             autoDelete: false,
                             arguments: null);
 }
Beispiel #3
0
        public static string CreateQueue(this IModel channel, RabbitMQExchangeOptions options)
        {
            var queue = channel.QueueDeclare(queue: options.QueueName,
                                             durable: options.Durable,
                                             exclusive: false,
                                             autoDelete: false,
                                             arguments: null);

            return(queue.QueueName);
        }
        private void BasicPublish(IModel channel, RabbitMQExchangeOptions options, byte[] body)
        {
            var properties = channel.CreateBasicProperties();

            properties.Persistent = true;

            channel.CreateModels(options);

            var routingKey = options.ExchangeType == "default" ? options.QueueName : options.RoutingKey;

            channel.BasicPublish(
                exchange: options.ExchangeName,
                routingKey: routingKey,
                basicProperties: properties,
                body: body);
        }
Beispiel #5
0
        public static void CreateModels(this IModel channel, RabbitMQExchangeOptions options, bool bindQueueToExchange = false)
        {
            if (options.ExchangeType == "default")
            {
                options.QueueName = channel.CreateQueue(options); // Garantir que o nome da fila seja propagado
            }
            else
            {
                channel.CreateExchange(options);

                if (bindQueueToExchange)
                {
                    options.QueueName = channel.CreateQueue(options);
                    channel.BindQueue(options);
                }
            }
        }
        private void ProxyPublish <T>(T model, string queueName = "") where T : BaseMessage
        {
            var json    = _serializer.Serialize(model);
            var encoded = Encoding.UTF8.GetBytes(json);

            using (var channel = _connection.CreateModel())
            {
                var options = RabbitMQExchangeOptions.Build <T>();

                if (!string.IsNullOrWhiteSpace(queueName))
                {
                    options.QueueName = queueName;
                }

                model.MessageId = Guid.NewGuid();

                BasicPublish(channel, options, encoded);
            }
        }
Beispiel #7
0
 public static void BindQueue(this IModel channel, RabbitMQExchangeOptions options)
 {
     channel.QueueBind(queue: options.QueueName,
                       exchange: options.ExchangeName,
                       routingKey: options.RoutingKey);
 }