Inheritance: RabbitMQ.Client.Events.EventingBasicConsumer, IRawConsumer
        public IRawConsumer CreateConsumer(IConsumerConfiguration cfg, IModel channel)
        {
            ConfigureQos(channel, cfg.PrefetchCount);
            var rawConsumer = new EventingRawConsumer(channel);

            rawConsumer.Received += (sender, args) =>
            {
                Task.Run(() =>
                {
                    if (_processedButNotAcked.Contains(args.BasicProperties.MessageId))
                    {
                        /*
                            This instance of the consumer has allready handled this message,
                            but something went wrong when 'ack'-ing the message, therefore
                            and the message was resent.
                        */
                        BasicAck(channel, args);
                        return;
                    }
                    _logger.LogInformation($"Message recived: MessageId: {args.BasicProperties.MessageId}");
                    rawConsumer
                        .OnMessageAsync(sender, args)
                        .ContinueWith(t =>
                        {
                            if (cfg.NoAck || rawConsumer.AcknowledgedTags.Contains(args.DeliveryTag))
                            {
                                return;
                            }
                            BasicAck(channel, args);
                        });
                });
            };

            return rawConsumer;
        }
Exemplo n.º 2
0
        public IRawConsumer CreateConsumer(IConsumerConfiguration cfg, IModel channel)
        {
            ConfigureQos(channel, cfg.PrefetchCount);
            var rawConsumer = new EventingRawConsumer(channel);

            rawConsumer.Received += (sender, args) =>
            {
                Task.Run(() =>
                {
                    if (_processedButNotAcked.Contains(args.BasicProperties.MessageId))
                    {
                        /*
                         *      This instance of the consumer has allready handled this message,
                         *      but something went wrong when 'ack'-ing the message, therefore
                         *      and the message was resent.
                         */
                        BasicAck(channel, args);
                        return;
                    }
                    _logger.LogInformation($"Message recived: MessageId: {args.BasicProperties.MessageId}");
                    rawConsumer
                    .OnMessageAsync(sender, args)
                    .ContinueWith(t =>
                    {
                        if (cfg.NoAck || rawConsumer.AcknowledgedTags.Contains(args.DeliveryTag))
                        {
                            return;
                        }
                        BasicAck(channel, args);
                    });
                });
            };

            return(rawConsumer);
        }
        public IRawConsumer CreateConsumer(IConsumerConfiguration cfg, IModel channel)
        {
            ConfigureQos(channel, cfg.PrefetchCount);
            var rawConsumer = new EventingRawConsumer(channel);

            _consumers.Add(rawConsumer);

            rawConsumer.Received += (sender, args) =>
            {
                Task.Run(() =>
                {
                    if (_processedButNotAcked.Contains(args.BasicProperties.MessageId))
                    {
                        /*
                            This instance of the consumer has allready handled this message,
                            but something went wrong when 'ack'-ing the message, therefore
                            and the message was resent.
                        */
                        BasicAck(channel, args, cfg);
                        return;
                    }
                    _logger.LogInformation($"Message recived: MessageId: {args.BasicProperties.MessageId}");
                    try
                    {
                        rawConsumer
                            .OnMessageAsync(sender, args)
                            .ContinueWith(t =>
                            {
                                if (t.IsFaulted)
                                {
                                    _logger.LogError($"An unhandled exception was caught for message {args.BasicProperties.MessageId}.\n", t.Exception);
                                    _strategy.OnRequestHandlerExceptionAsync(rawConsumer, cfg, args, t.Exception);
                                    return;
                                }
                                if (cfg.NoAck || rawConsumer.NackedDeliveryTags.Contains(args.DeliveryTag))
                                {
                                /*
                                    The consumer has stated that 'ack'-ing is not required, so
                                    now that the message is handled, the consumer is done.
                                */
                                    return;
                                }

                                BasicAck(channel, args, cfg);
                        });
                    }
                    catch (Exception e)
                    {
                        _logger.LogError($"An unhandled exception was caught for message {args.BasicProperties.MessageId}.\n", e);
                        _strategy.OnRequestHandlerExceptionAsync(rawConsumer, cfg, args, e);
                    }
                });
            };

            return rawConsumer;
        }
        public IRawConsumer CreateConsumer(IConsumerConfiguration cfg, IModel channel)
        {
            ConfigureQos(channel, cfg.PrefetchCount);
            var rawConsumer = new EventingRawConsumer(channel);

            _consumers.Add(rawConsumer);

            rawConsumer.Received += (sender, args) =>
            {
                if (_processedButNotAcked.Contains(args.BasicProperties.MessageId))
                {
                    /*
                        This instance of the consumer has allready handled this message,
                        but something went wrong when 'ack'-ing the message, therefore
                        and the message was resent.
                    */
                    BasicAck(channel, args, cfg);
                    return;
                }

                Task onMessageTask;
                try
                {
                    _logger.LogInformation($"Message recived: MessageId: {args.BasicProperties.MessageId}");
                    onMessageTask = rawConsumer.OnMessageAsync(sender, args);
                }
                catch (Exception)
                {
                    /*
                        The message handler threw an exception. It is time to hand over the
                        message handling to an error strategy instead.
                    */
                    if (!cfg.NoAck || rawConsumer.NackedDeliveryTags.Contains(args.DeliveryTag))
                    {
                        BasicAck(channel, args, cfg); // TODO: employ error handling strategy instead
                    }
                    return;
                }
                onMessageTask
                    .ContinueWith(t =>
                    {
                        if (cfg.NoAck || rawConsumer.NackedDeliveryTags.Contains(args.DeliveryTag))
                        {
                            /*
                                The consumer has stated that 'ack'-ing is not required, so
                                now that the message is handled, the consumer is done.
                            */
                            return;
                        }

                        BasicAck(channel, args, cfg);
                    });
            };

            return rawConsumer;
        }