/// <summary>
        /// Stop the channel.
        /// </summary>
        public void Stop()
        {
            this.cancelled.LazySet(true);
            if (this.consumer != null && this.consumer.Model != null && this.consumer.ConsumerTag != null && !this.cancelReceived.Value)
            {
                try
                {
                    RabbitUtils.CloseMessageConsumer(this.consumer.Model, this.consumer.ConsumerTag, this.transactional);
                }
                catch (Exception ex)
                {
                    Logger.Debug(m => m("Error closing consumer"), ex);
                }
            }

            Logger.Debug("Closing Rabbit Channel: " + this.channel);
            RabbitUtils.SetPhysicalCloseRequired(true);

            // This one never throws exceptions...
            RabbitUtils.CloseChannel(this.channel);
            this.deliveryTags.Clear();
            this.consumer = null;
        }
        public void Start()
        {
            this.channel = ConnectionFactoryUtils.GetTransactionalResourceHolder((IConnectionFactory)connectionFactory, transactional).Channel;
            this.consumer = new InternalConsumer(this.channel, this);
            this.activeObjectCounter.Add(this);
            try
            {
                // Set basicQos before calling basicConsume (it is ignored if we are not transactional and the broker will
                // send blocks of 100 messages)
                this.channel.BasicQos((uint)this.prefetchSize, (ushort)this.prefetchCount, false);
                foreach (var t in this.queues)
                {
                    this.channel.QueueDeclarePassive(t);
                }
            }
            catch (Exception e)
            {
                this.activeObjectCounter.Release(this);
                throw new Exception("Cannot prepare queue for listener. " + "Either the queue doesn't exist or the broker will not allow us to use it.", e);
            }

            try
            {
                foreach (var t in this.queues)
                {
                    this.channel.BasicConsume(t, this.acknowledgeMode.IsAutoAck(), this.consumer);
                    if (this.logger.IsDebugEnabled)
                    {
                        this.logger.Debug("Started " + this);
                    }
                }
            }
            catch (Exception e)
            {
                throw RabbitUtils.ConvertRabbitAccessException(e);
            }
        }
        /// <summary>
        /// Stop the channel.
        /// </summary>
        public void Stop()
        {
            this.cancelled.LazySet(true);
            if (this.consumer != null && this.consumer.Model != null && this.consumer.ConsumerTag != null && !this.cancelReceived.Value)
            {
                try
                {
                    RabbitUtils.CloseMessageConsumer(this.consumer.Model, this.consumer.ConsumerTag, this.transactional);
                }
                catch (Exception ex)
                {
                    Logger.Debug(m => m("Error closing consumer"), ex);
                }
            }

            Logger.Debug("Closing Rabbit Channel: " + this.channel);
            RabbitUtils.SetPhysicalCloseRequired(true);

            // This one never throws exceptions...
            RabbitUtils.CloseChannel(this.channel);
            this.deliveryTags.Clear();
            this.consumer = null;
        }
        /// <summary>The start.</summary>
        public void Start()
        {
            Logger.Debug(m => m("Starting consumer {0}", this));

            this.channel  = ConnectionFactoryUtils.GetTransactionalResourceHolder(this.connectionFactory, this.transactional).Channel;
            this.consumer = new InternalConsumer(this.channel, this);
            this.deliveryTags.Clear();
            this.activeObjectCounter.Add(this);
            var passiveDeclareTries = 3; // mirrored queue might be being moved

            while (passiveDeclareTries > 0)
            {
                passiveDeclareTries--;
                try
                {
                    if (!this.acknowledgeMode.IsAutoAck())
                    {
                        // Set basicQos before calling basicConsume (otherwise if we are not acking the broker
                        // will send blocks of 100 messages)
                        // The Java client includes a convenience method BasicQos(ushort prefetchCount), which sets 0 as the prefetchSize and false as global
                        this.channel.BasicQos(0, (ushort)this.prefetchCount, false);
                    }

                    foreach (var t in this.queues)
                    {
                        this.channel.QueueDeclarePassive(t);
                    }

                    passiveDeclareTries = 0;
                }
                catch (Exception e)
                {
                    if (passiveDeclareTries > 0 && this.channel.IsOpen)
                    {
                        Logger.Warn(m => m("Reconnect failed; retries left=" + (passiveDeclareTries - 1)), e);
                        try
                        {
                            Thread.Sleep(5000);
                        }
                        catch (ThreadInterruptedException e1)
                        {
                            Thread.CurrentThread.Interrupt();
                        }
                    }
                    else
                    {
                        this.activeObjectCounter.Release(this);
                        throw new FatalListenerStartupException("Cannot prepare queue for listener. Either the queue doesn't exist or the broker will not allow us to use it.", e);
                    }
                }
            }

            try
            {
                foreach (var t in this.queues)
                {
                    this.channel.BasicConsume(t, this.acknowledgeMode.IsAutoAck(), this.consumer);
                    Logger.Debug(m => m("Started on queue '{0}': {1}", t, this));
                }
            }
            catch (Exception e)
            {
                throw RabbitUtils.ConvertRabbitAccessException(e);
            }
        }
        /// <summary>The start.</summary>
        public void Start()
        {
            Logger.Debug(m => m("Starting consumer {0}", this));

            this.channel = ConnectionFactoryUtils.GetTransactionalResourceHolder(this.connectionFactory, this.transactional).Channel;
            this.consumer = new InternalConsumer(this.channel, this);
            this.deliveryTags.Clear();
            this.activeObjectCounter.Add(this);
            var passiveDeclareTries = 3; // mirrored queue might be being moved
            while (passiveDeclareTries > 0)
            {
                passiveDeclareTries--;
                try
                {
                    if (!this.acknowledgeMode.IsAutoAck())
                    {
                        // Set basicQos before calling basicConsume (otherwise if we are not acking the broker
                        // will send blocks of 100 messages)
                        // The Java client includes a convenience method BasicQos(ushort prefetchCount), which sets 0 as the prefetchSize and false as global
                        this.channel.BasicQos(0, (ushort)this.prefetchCount, false);
                    }

                    foreach (var t in this.queues)
                    {
                        this.channel.QueueDeclarePassive(t);
                    }

                    passiveDeclareTries = 0;
                }
                catch (Exception e)
                {
                    if (passiveDeclareTries > 0 && this.channel.IsOpen)
                    {
                        Logger.Warn(m => m("Reconnect failed; retries left=" + (passiveDeclareTries - 1)), e);
                        try
                        {
                            Thread.Sleep(5000);
                        }
                        catch (ThreadInterruptedException e1)
                        {
                            Thread.CurrentThread.Interrupt();
                        }
                    }
                    else
                    {
                        this.activeObjectCounter.Release(this);
                        throw new FatalListenerStartupException("Cannot prepare queue for listener. Either the queue doesn't exist or the broker will not allow us to use it.", e);
                    }
                }
            }

            try
            {
                foreach (var t in this.queues)
                {
                    this.channel.BasicConsume(t, this.acknowledgeMode.IsAutoAck(), this.consumer);
                    Logger.Debug(m => m("Started on queue '{0}': {1}", t, this));
                }
            }
            catch (Exception e)
            {
                throw RabbitUtils.ConvertRabbitAccessException(e);
            }
        }