internal void WaitAndHandleMessageDelivery(Action<BasicDeliverEventArgs> handler)
{
try
{
BasicDeliverEventArgs deliverEventArgs = null;
lock (_sharedQueueLock)
{
#if DEBUG
_watcher.DebugFormat("1. Wait the semaphore to release");
_pool.WaitOne();
_watcher.DebugFormat("2. Semaphore released, wait a msg from RabbitMQ. Probably a wait-for-ack message is blocking this");
#else
_pool.WaitOne();
#endif
if (_status == ConsumerStatus.Active)
{
deliverEventArgs = Dequeue();
}
#if DEBUG
_watcher.DebugFormat("3. Msg from RabbitMQ arrived (probably the previous msg has been acknownledged), prepare to handle it");
#endif
}
if (deliverEventArgs != null)
{
lock (Subscription.OutstandingDeliveryTags)
{
Subscription.OutstandingDeliveryTags[deliverEventArgs.ConsumerTag].Add(deliverEventArgs.DeliveryTag);
}
handler(deliverEventArgs);
}
else
{
_watcher.ErrorFormat("Message arrived but it's null for some reason, properly a serious BUG :D, contact author asap, release semaphore for other messages");
_pool.Release();
}
}
catch (EndOfStreamException)
{
// This thread will be ended soon because the new consumer will be created
// do nothing here, EOS fired when queue is closed
// Looks like the connection has gone away, so wait a little while
// before continuing to poll the queue
Thread.Sleep(100);
#if DEBUG
_watcher.DebugFormat("EndOfStreamException occurs, release the semaphore for another message");
#endif
_pool.Release();
}
}