[rabbitmq-discuss] Problem Extending DefaultBasicConsumer Class

Rom Cabral rom.t.cabral at gmail.com
Tue Sep 16 15:47:35 BST 2014


Hi guys,

I'm in the process of exploring RabbitMQ so my probIem might be very easy 
to some of you. Anyway, I decided to post this question after long hours of 
debugging my code. Basically, I have 2 problems that I noticed when I try 
extending DefaultConsumer. 

First is that, acknowledgement is not working, message is moved back from 
unacknowledge to ready upon closing the channel. Second is that I'm 
encountering "AlreadyClosedException" when trying to close the connection. 
I'm wonder why this is happening even if I have checking to close only open 
connection. Hope to hear your comments and suggestions.

class SampleConsumer : DefaultBasicConsumer
    {

        public SampleConsumer(IModel channel) : base(channel)
        {
        }

        public override void HandleBasicDeliver(string consumerTag, ulong 
deliveryTag, bool redelivered, string exchange, string routingKey,
            IBasicProperties properties, byte[] body)
        {
            try
            {
                Console.WriteLine(Encoding.ASCII.GetString(body));
                Model.BasicAck(deliveryTag, false);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Model.BasicNack(deliveryTag, false, false);
                
            }
            //base.HandleBasicDeliver(consumerTag, deliveryTag, 
redelivered, exchange, routingKey, properties, body);
        }
    }
}

class Receiver : IDisposable
{
// create xchange, queue and binding here
public void Subscribe(string queue)
        {
            var consumer = new SampleConsumer(Channel);
            Channel.BasicConsume(queue, false, consumer);
        }

    public void Dispose()
        {
            if (Channel != null && Channel.IsOpen)
                Channel.Close();

            if (Connection != null && Connection.IsOpen)
                Connection.Close();
        }
}

Main
  using(var r = new Receiver(server...)
     r.Subscribe(queue);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20140916/fe29ace0/attachment.html>


More information about the rabbitmq-discuss mailing list