Exemplo n.º 1
0
        private async void Consumer_Received(object sender, BasicDeliverEventArgs e)
        {
            _diagnosticSource.BeforeReceived(sender, e);

            var eventName = e.RoutingKey;

            var message = Encoding.UTF8.GetString(e.Body);

            try
            {
                await ProcessEventAsync(eventName, message);
            }
            catch (RabbitMQEventBusUnInitializedException error)
            {
                _logger.LogWarning(0, error, "will retry 1 seconds later");

                // 等待一秒钟后拒绝事件,进行重试
                await Task.Delay(1000);

                _channel.BasicNack(e.DeliveryTag, false, true);

                return;
            }
            catch (Exception error)
            {
                _logger.LogError(0, error, "A error was occur during handing event,please check the consumer state");

                await Task.Delay(1000);

                _channel.BasicNack(e.DeliveryTag, false, true);
                return;
            }
            _channel.BasicAck(e.DeliveryTag, false);

            _diagnosticSource.AfterReceived(e);
        }