public class QueueingBasicConsumer

Summary

Simple IBasicConsumer implementation that uses a SharedQueue to buffer incoming deliveries.

Remarks

Received messages are placed in the SharedQueue as instances of BasicDeliverEventArgs.

Note that messages taken from the SharedQueue may need acknowledging with IModel.BasicAck.

When the consumer is closed, through BasicCancel or through the shutdown of the underlying IModel or IConnection, the SharedQueue's Close() method is called, which causes any threads blocked on the queue's Enqueue() or Dequeue() operations to throw EndOfStreamException (see the comment for SharedQueue.Close()).

The following is a simple example of the usage of this class:

            	IModel channel = ...;
            	QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
            	channel.BasicConsume(ticket, queueName, null, consumer);
            	
            	// At this point, messages will be being asynchronously delivered,
            	// and will be queueing up in consumer.Queue.
            	
            	while (true) {
            	    try {
            	        BasicDeliverEventArgs e = (BasicDeliverEventArgs) consumer.Queue.Dequeue();
            	        // ... handle the delivery ...
            	        channel.BasicAck(e.DeliveryTag, false);
            	    } catch (EndOfStreamException ex) {
            	        // The consumer was cancelled, the model closed, or the
            	        // connection went away.
            	        break;
            	    }
            	}
            

Property Summary

Flags Type Name Summary
public SharedQueue Queue (r)

Retrieves the SharedQueue that messages arrive on.

Constructor Summary

Flags Name Summary
public QueueingBasicConsumer(IModel model, SharedQueue queue)

Creates a fresh QueueingBasicConsumer, initialising the Model and Queue properties to the given values.

public QueueingBasicConsumer(IModel model)

Creates a fresh QueueingBasicConsumer, with Model set to the argument, and Queue set to a fresh SharedQueue.

public QueueingBasicConsumer()

Creates a fresh QueueingBasicConsumer, initialising the Model property to null and the Queue property to a fresh SharedQueue.

Method Summary

Flags Name Summary
public virtual void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body)

Overrides DefaultBasicConsumer's HandleBasicDeliver implementation, building a BasicDeliverEventArgs instance and placing it in the Queue.

public virtual void OnCancel()

Overrides DefaultBasicConsumer's OnCancel implementation, extending it to call the Close() method of the SharedQueue.

Property Detail

public SharedQueue Queue (r)

Summary

Retrieves the SharedQueue that messages arrive on.

Constructor Detail

QueueingBasicConsumer

public QueueingBasicConsumer(IModel model, SharedQueue queue)

Parameters
Name Type
model IModel
queue SharedQueue

Summary

Creates a fresh QueueingBasicConsumer, initialising the Model and Queue properties to the given values.

QueueingBasicConsumer

public QueueingBasicConsumer(IModel model)

Parameters
Name Type
model IModel

Summary

Creates a fresh QueueingBasicConsumer, with Model set to the argument, and Queue set to a fresh SharedQueue.

QueueingBasicConsumer

public QueueingBasicConsumer()

Summary

Creates a fresh QueueingBasicConsumer, initialising the Model property to null and the Queue property to a fresh SharedQueue.

Method Detail

HandleBasicDeliver

public virtual void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body)

Flags public virtual
Return type void
Parameters
Name Type
consumerTag string
deliveryTag ulong
redelivered bool
exchange string
routingKey string
properties IBasicProperties
body byte[]

Summary

Overrides DefaultBasicConsumer's HandleBasicDeliver implementation, building a BasicDeliverEventArgs instance and placing it in the Queue.

OnCancel

public virtual void OnCancel()

Flags public virtual
Return type void

Summary

Overrides DefaultBasicConsumer's OnCancel implementation, extending it to call the Close() method of the SharedQueue.