1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
6 //---------------------------------------------------------------------------
7 // Copyright (C) 2007-2009 LShift Ltd., Cohesive Financial
8 // Technologies LLC., and Rabbit Technologies Ltd.
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
14 // http://www.apache.org/licenses/LICENSE-2.0
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //---------------------------------------------------------------------------
25 //---------------------------------------------------------------------------
26 // The contents of this file are subject to the Mozilla Public License
27 // Version 1.1 (the "License"); you may not use this file except in
28 // compliance with the License. You may obtain a copy of the License at
29 // http://www.rabbitmq.com/mpl.html
31 // Software distributed under the License is distributed on an "AS IS"
32 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
33 // License for the specific language governing rights and limitations
36 // The Original Code is The RabbitMQ .NET Client.
38 // The Initial Developers of the Original Code are LShift Ltd,
39 // Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
41 // Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
42 // Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
43 // are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
44 // Technologies LLC, and Rabbit Technologies Ltd.
46 // Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift
47 // Ltd. Portions created by Cohesive Financial Technologies LLC are
48 // Copyright (C) 2007-2009 Cohesive Financial Technologies
49 // LLC. Portions created by Rabbit Technologies Ltd are Copyright
50 // (C) 2007-2009 Rabbit Technologies Ltd.
52 // All Rights Reserved.
54 // Contributor(s): ______________________________________.
56 //---------------------------------------------------------------------------
60 using RabbitMQ.Client.Events;
62 namespace RabbitMQ.Client
64 ///<summary>Simple IBasicConsumer implementation that uses a
65 ///SharedQueue to buffer incoming deliveries.</summary>
68 /// Received messages are placed in the SharedQueue as instances
69 /// of BasicDeliverEventArgs.
72 /// Note that messages taken from the SharedQueue may need
73 /// acknowledging with IModel.BasicAck.
76 /// When the consumer is closed, through BasicCancel or through
77 /// the shutdown of the underlying IModel or IConnection, the
78 /// SharedQueue's Close() method is called, which causes any
79 /// threads blocked on the queue's Enqueue() or Dequeue()
80 /// operations to throw EndOfStreamException (see the comment for
81 /// SharedQueue.Close()).
84 /// The following is a simple example of the usage of this class:
87 /// IModel channel = ...;
88 /// QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
89 /// channel.BasicConsume(queueName, null, consumer);
91 /// // At this point, messages will be being asynchronously delivered,
92 /// // and will be queueing up in consumer.Queue.
96 /// BasicDeliverEventArgs e = (BasicDeliverEventArgs) consumer.Queue.Dequeue();
97 /// // ... handle the delivery ...
98 /// channel.BasicAck(e.DeliveryTag, false);
99 /// } catch (EndOfStreamException ex) {
100 /// // The consumer was cancelled, the model closed, or the
101 /// // connection went away.
107 public class QueueingBasicConsumer : DefaultBasicConsumer
109 protected SharedQueue m_queue;
111 ///<summary>Creates a fresh QueueingBasicConsumer,
112 ///initialising the Model property to null and the Queue
113 ///property to a fresh SharedQueue.</summary>
114 public QueueingBasicConsumer() : this(null) { }
116 ///<summary>Creates a fresh QueueingBasicConsumer, with Model
117 ///set to the argument, and Queue set to a fresh
118 ///SharedQueue.</summary>
119 public QueueingBasicConsumer(IModel model) : this(model, new SharedQueue()) { }
121 ///<summary>Creates a fresh QueueingBasicConsumer,
122 ///initialising the Model and Queue properties to the given
124 public QueueingBasicConsumer(IModel model, SharedQueue queue)
130 ///<summary>Retrieves the SharedQueue that messages arrive on.</summary>
131 public SharedQueue Queue
133 get { return m_queue; }
136 ///<summary>Overrides DefaultBasicConsumer's OnCancel
137 ///implementation, extending it to call the Close() method of
138 ///the SharedQueue.</summary>
139 public override void OnCancel()
145 ///<summary>Overrides DefaultBasicConsumer's
146 ///HandleBasicDeliver implementation, building a
147 ///BasicDeliverEventArgs instance and placing it in the
149 public override void HandleBasicDeliver(string consumerTag,
154 IBasicProperties properties,
157 BasicDeliverEventArgs e = new BasicDeliverEventArgs();
158 e.ConsumerTag = consumerTag;
159 e.DeliveryTag = deliveryTag;
160 e.Redelivered = redelivered;
161 e.Exchange = exchange;
162 e.RoutingKey = routingKey;
163 e.BasicProperties = properties;