Skip to content

Commit 7615a58

Browse files
Add Subscription#Nack
1 parent 6cff782 commit 7615a58

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

projects/client/RabbitMQ.Client/src/client/messagepatterns/Subscription.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,44 @@ public void Ack(BasicDeliverEventArgs evt)
187187
}
188188
}
189189

190+
///<summary>If LatestEvent is non-null, passes it to
191+
///Nack(BasicDeliverEventArgs, multiple, requeue). Causes LatestEvent to become
192+
///null.</summary>
193+
public void Nack(bool multiple, bool requeue)
194+
{
195+
if (m_latestEvent != null) {
196+
Nack(m_latestEvent, multiple, requeue);
197+
}
198+
}
199+
200+
///<summary>If we are not in "noAck" mode, calls
201+
///IModel.BasicNack with the delivery-tag from the passed in
202+
///event; otherwise, sends nothing to the server. In both
203+
///cases, if the passed-in event is the same as LatestEvent
204+
///(by pointer comparison), sets LatestEvent to
205+
///null.</summary>
206+
///<remarks>
207+
/// Make sure that this method is only called with events that
208+
/// originated from this Subscription - other usage will have
209+
/// unpredictable results.
210+
///</remarks>
211+
public void Nack(BasicDeliverEventArgs evt,
212+
bool multiple,
213+
bool requeue)
214+
{
215+
if (evt == null) {
216+
return;
217+
}
218+
219+
if (!m_noAck) {
220+
m_model.BasicNack(evt.DeliveryTag, multiple, requeue);
221+
}
222+
223+
if (evt == m_latestEvent) {
224+
m_latestEvent = null;
225+
}
226+
}
227+
190228
///<summary>Retrieves the next incoming delivery in our
191229
///subscription queue.</summary>
192230
///<remarks>

projects/client/Unit/src/unit/TestMessagePatternsSubscription.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,20 @@ public void TestSubscriptionAck()
9090
QueueDeclareOk ok = Model.QueueDeclarePassive(q);
9191
Assert.AreEqual(0, ok.MessageCount);
9292
}
93+
94+
[Test]
95+
public void TestSubscriptionNack()
96+
{
97+
Model.BasicQos(0, 1, false);
98+
string q = Model.QueueDeclare();
99+
Subscription sub = new Subscription(Model, q, false);
100+
101+
Model.BasicPublish("", q, null, enc.GetBytes("a message"));
102+
BasicDeliverEventArgs res = sub.Next();
103+
Assert.IsNotNull(res);
104+
sub.Nack(false, false);
105+
QueueDeclareOk ok = Model.QueueDeclarePassive(q);
106+
Assert.AreEqual(0, ok.MessageCount);
107+
}
93108
}
94109
}

0 commit comments

Comments
 (0)