private void SetQosAndCreateConsumers()
        {
            if (!AcknowledgeMode.IsAutoAck() && !Cancelled)
            {
                // Set basicQos before calling basicConsume (otherwise if we are not acking the broker
                // will send blocks of 100 messages)
                try
                {
                    Channel.BasicQos(0, PrefetchCount, true);
                }
                catch (Exception e)
                {
                    ActiveObjectCounter.Release(this);
                    throw new RabbitIOException(e);
                }
            }

            try
            {
                if (!Cancelled)
                {
                    foreach (var queueName in Queues)
                    {
                        if (!MissingQueues.Contains(queueName))
                        {
                            ConsumeFromQueue(queueName);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw RabbitExceptionTranslator.ConvertRabbitAccessException(e);
            }
        }
        private void HandleDeclarationException(int passiveDeclareRetries, DeclarationException e)
        {
            if (passiveDeclareRetries > 0 && Channel.IsOpen)
            {
                Logger?.LogWarning(e, "Queue declaration failed; retries left={retries}", passiveDeclareRetries);
                try
                {
                    Thread.Sleep(FailedDeclarationRetryInterval);
                }
                catch (Exception e1)
                {
                    Declaring = false;
                    ActiveObjectCounter.Release(this);
                    throw RabbitExceptionTranslator.ConvertRabbitAccessException(e1);
                }
            }
            else if (e.FailedQueues.Count < Queues.Count)
            {
                Logger?.LogWarning("Not all queues are available; only listening on those that are - configured: {queues}; not available: {notavail}", string.Join(',', Queues), string.Join(',', e.FailedQueues));
                lock (MissingQueues)
                {
                    foreach (var q in e.FailedQueues)
                    {
                        MissingQueues.Add(q);
                    }
                }

                LastRetryDeclaration = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            }
            else
            {
                Declaring = false;
                ActiveObjectCounter.Release(this);
                throw new QueuesNotAvailableException("Cannot prepare queue for listener. Either the queue doesn't exist or the broker will not allow us to use it.", e);
            }
        }
        private void CheckMissingQueues()
        {
            var now = DateTimeOffset.Now.ToUnixTimeMilliseconds();

            if (now - RetryDeclarationInterval > LastRetryDeclaration)
            {
                lock (MissingQueues)
                {
                    List <string> toRemove = new List <string>();
                    Exception     error    = null;
                    foreach (var queueToCheck in MissingQueues)
                    {
                        bool        available       = true;
                        IConnection connection      = null;
                        RC.IModel   channelForCheck = null;
                        try
                        {
                            channelForCheck = ConnectionFactory.CreateConnection().CreateChannel(false);
                            channelForCheck.QueueDeclarePassive(queueToCheck);
                            Logger?.LogInformation("Queue '{queue}' is now available", queueToCheck);
                        }
                        catch (Exception e)
                        {
                            available = false;
                            Logger?.LogWarning(e, "Queue '{queue}' is not available", queueToCheck);
                        }
                        finally
                        {
                            RabbitUtils.CloseChannel(channelForCheck);
                            RabbitUtils.CloseConnection(connection);
                        }

                        if (available)
                        {
                            try
                            {
                                ConsumeFromQueue(queueToCheck);
                                toRemove.Add(queueToCheck);
                            }
                            catch (Exception e)
                            {
                                error = e;
                                break;
                            }
                        }
                    }

                    if (toRemove.Count > 0)
                    {
                        foreach (var remove in toRemove)
                        {
                            MissingQueues.Remove(remove);
                        }
                    }

                    if (error != null)
                    {
                        throw RabbitExceptionTranslator.ConvertRabbitAccessException(error);
                    }
                }

                LastRetryDeclaration = now;
            }
        }