[rabbitmq-discuss] Best way to subscribe-unsubscribe-subscribe

Valentin Polezhaev valentin.polezhaev at gmail.com
Thu Aug 22 09:36:30 BST 2013


Hello,
I am new in Rabbit and I need to write consumer code which can 1) subscribe 
to queue with specified name, 2) recieve messages, 3) if another queue name 
is specified switch to new queue and start to recieve messages from it 4) 
go to step 2).

I use official .NET client and my code looks like:

\\consumer:

private string _currentQueue;

public T Consume(string queue)
{
    ...
    if (_currentQueue != queue)                
       Subscribe(queue);
    ... 
    var ea = (BasicDeliverEventArgs) _basicConsumer.Queue.Dequeue();
    ...               
}

private void Subscribe(string queue)
{
        _currentQueue = queue;
        if (_basicConsumer.IsRunning)
        {                
            _channel.BasicCancel(_basicConsumer.ConsumerTag);               
 
            _basicConsumer = new QueueingBasicConsumer(_channel) { 
ConsumerTag = GetNewTag()};
         }
         _channel.BasicConsume(_currentQueue, false, _basicConsumer);
}

For example I have 2 queues ("A", "B") with 2 messages in each and I 
consume in A-B-A-B order.
My code successfully reads first messages from both A and B queues, but 
third time (second read from A) it stays at line
var ea = (BasicDeliverEventArgs) _basicConsumer.Queue.Dequeue();
and nothing happens.

Moreover it'is strange that via control panel I can see 2 unacked messages 
in both A and B queues. Corresponding to program beheaviour I can 
understand unacked message in A queue but not in B - second consuming from 
B was not started yet.

Please, tell me the best way to do this.

I consider that simple reconnection will solve my problem, but seems it's a 
bad practice.

Thanks a lot.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20130822/da54876e/attachment.htm>


More information about the rabbitmq-discuss mailing list