a5c0347998616ec13c73538fa0128f4249da6245
[mono.git] / mcs / class / RabbitMQ.Client / src / client / api / QueueingBasicConsumer.cs
1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
3 //
4 // The APL v2.0:
5 //
6 //---------------------------------------------------------------------------
7 //   Copyright (C) 2007-2009 LShift Ltd., Cohesive Financial
8 //   Technologies LLC., and Rabbit Technologies Ltd.
9 //
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
13 //
14 //       http://www.apache.org/licenses/LICENSE-2.0
15 //
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 //---------------------------------------------------------------------------
22 //
23 // The MPL v1.1:
24 //
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
30 //
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
34 //   under the License.
35 //
36 //   The Original Code is The RabbitMQ .NET Client.
37 //
38 //   The Initial Developers of the Original Code are LShift Ltd,
39 //   Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
40 //
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.
45 //
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.
51 //
52 //   All Rights Reserved.
53 //
54 //   Contributor(s): ______________________________________.
55 //
56 //---------------------------------------------------------------------------
57 using System;
58
59 using RabbitMQ.Util;
60 using RabbitMQ.Client.Events;
61
62 namespace RabbitMQ.Client
63 {
64     ///<summary>Simple IBasicConsumer implementation that uses a
65     ///SharedQueue to buffer incoming deliveries.</summary>
66     ///<remarks>
67     ///<para>
68     /// Received messages are placed in the SharedQueue as instances
69     /// of BasicDeliverEventArgs.
70     ///</para>
71     ///<para>
72     /// Note that messages taken from the SharedQueue may need
73     /// acknowledging with IModel.BasicAck.
74     ///</para>
75     ///<para>
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()).
82     ///</para>
83     ///<para>
84     /// The following is a simple example of the usage of this class:
85     ///</para>
86     ///<example><code>
87     /// IModel channel = ...;
88     /// QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
89     /// channel.BasicConsume(queueName, null, consumer);
90     /// 
91     /// // At this point, messages will be being asynchronously delivered,
92     /// // and will be queueing up in consumer.Queue.
93     /// 
94     /// while (true) {
95     ///     try {
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.
102     ///         break;
103     ///     }
104     /// }
105     ///</code></example>
106     ///</remarks>
107     public class QueueingBasicConsumer : DefaultBasicConsumer
108     {
109         protected SharedQueue m_queue;
110
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) { }
115
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()) { }
120
121         ///<summary>Creates a fresh QueueingBasicConsumer,
122         ///initialising the Model and Queue properties to the given
123         ///values.</summary>
124         public QueueingBasicConsumer(IModel model, SharedQueue queue)
125             : base(model)
126         {
127             m_queue = queue;
128         }
129
130         ///<summary>Retrieves the SharedQueue that messages arrive on.</summary>
131         public SharedQueue Queue
132         {
133             get { return m_queue; }
134         }
135
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()
140         {
141             m_queue.Close();
142             base.OnCancel();
143         }
144
145         ///<summary>Overrides DefaultBasicConsumer's
146         ///HandleBasicDeliver implementation, building a
147         ///BasicDeliverEventArgs instance and placing it in the
148         ///Queue.</summary>
149         public override void HandleBasicDeliver(string consumerTag,
150                                                 ulong deliveryTag,
151                                                 bool redelivered,
152                                                 string exchange,
153                                                 string routingKey,
154                                                 IBasicProperties properties,
155                                                 byte[] body)
156         {
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;
164             e.Body = body;
165             m_queue.Enqueue(e);
166         }
167     }
168 }