private Thread BuildListenerThread()
{
return(new Thread(() =>
{
while (!isDisposing)
{
BasicDeliverEventArgs item = null;
// grab the first item where any of the consumer queues has a value
var queue = Queues
.FirstOrDefault(q => q.TryTake(out item));
// if we're dead and we didn't find an item,
// it means it's time to pack up and go home
if (isDisposing && queue == null)
{
break;
}
// if we're not dead and we didn't find an item
// lets go around again
if (!isDisposing && queue == null)
{
continue;
}
if (item == null)
{
continue;
}
var delivery = new Delivery(
item.DeliveryTag,
item.Body,
item.RoutingKey,
item.Exchange,
item.BasicProperties.ContentType,
item.BasicProperties.CorrelationId,
item.BasicProperties.ReplyTo,
item.BasicProperties.Headers);
// register the queue as a delivery listener on the delivery so that
// we can ack or nack the message depending on whether or not it was processed
delivery.RegisterDeliveryListener(queue);
// deliver the message to the delivery strategy
deliveryStrategy.Deliver(delivery);
}
logger.DebugFormat("Subscription thread terminated for: {0}", this);
}));
}