Ejemplo n.º 1
0
        private async Task HandleUnexpectedShutdownAsync(ShutdownEventArgs e)
        {
            if (!_shutdown)
            {
                await Task.Yield();

                bool success;
                do
                {
                    await _chanHost.MakeChannelAsync();

                    _logger.LogWarning(
                        LogMessages.Consumers.ConsumerShutdownEvent,
                        ConsumerOptions.ConsumerName,
                        e.ReplyText);

                    success = StartConsuming();
                }while (!_shutdown && !success);
            }
        }
Ejemplo n.º 2
0
        private async Task <bool> StartConsumingAsync()
        {
            if (_shutdown)
            {
                return(false);
            }

            _logger.LogInformation(
                LogMessages.Consumers.StartingConsumer,
                ConsumerOptions.ConsumerName);

            if (Options.FactoryOptions.EnableDispatchConsumersAsync)
            {
                if (_asyncConsumer != null)
                {
                    _asyncConsumer.Received -= ReceiveHandlerAsync;
                    _asyncConsumer.Shutdown -= ConsumerShutdownAsync;
                }

                try
                {
                    _asyncConsumer = CreateAsyncConsumer();
                    if (_asyncConsumer == null)
                    {
                        return(false);
                    }

                    _chanHost
                    .GetChannel()
                    .BasicConsume(
                        ConsumerOptions.QueueName,
                        ConsumerOptions.AutoAck ?? false,
                        ConsumerOptions.ConsumerName,
                        ConsumerOptions.NoLocal ?? false,
                        ConsumerOptions.Exclusive ?? false,
                        null,
                        _asyncConsumer);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Exception creating internal RabbitMQ consumer. Retrying...");
                    await Task.Delay(1000).ConfigureAwait(false);

                    await _chanHost.MakeChannelAsync().ConfigureAwait(false);

                    return(false);
                }
            }
            else
            {
                if (_consumer != null)
                {
                    _consumer.Received -= ReceiveHandler;
                    _consumer.Shutdown -= ConsumerShutdown;
                }

                try
                {
                    _consumer = CreateConsumer();
                    if (_consumer == null)
                    {
                        return(false);
                    }

                    _chanHost
                    .GetChannel()
                    .BasicConsume(
                        ConsumerOptions.QueueName,
                        ConsumerOptions.AutoAck ?? false,
                        ConsumerOptions.ConsumerName,
                        ConsumerOptions.NoLocal ?? false,
                        ConsumerOptions.Exclusive ?? false,
                        null,
                        _consumer);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Exception creating internal RabbitMQ consumer. Retrying...");
                    await Task.Delay(1000).ConfigureAwait(false);

                    await _chanHost.MakeChannelAsync().ConfigureAwait(false);

                    return(false);
                }
            }

            _logger.LogInformation(
                LogMessages.Consumers.StartedConsumer,
                ConsumerOptions.ConsumerName);

            return(true);
        }