Skip to content

Commit f423adc

Browse files
author
Emile Joubert
committed
Consumer cancellation event
1 parent c92cf59 commit f423adc

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

projects/client/ApigenBootstrap/RabbitMQ.Client.ApigenBootstrap.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@
108108
<Compile Include="..\RabbitMQ.Client\src\client\events\FlowControlEventHandler.cs">
109109
<Link>src\client\events\FlowControlEventHandler.cs</Link>
110110
</Compile>
111+
<Compile Include="..\RabbitMQ.Client\src\client\events\ConsumerCancelledEventHandler.cs">
112+
<Link>src\client\events\ConsumerCancelledEventHandler.cs</Link>
113+
</Compile>
114+
<Compile Include="..\RabbitMQ.Client\src\client\events\ConsumerEventArgs.cs">
115+
<Link>src\client\events\ConsumerEventArgs.cs</Link>
116+
</Compile>
111117
<Compile Include="properties\AssemblyInfo.cs" />
112118
</ItemGroup>
113119

projects/client/RabbitMQ.Client/src/client/api/DefaultBasicConsumer.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
//---------------------------------------------------------------------------
4040

4141
using System;
42+
using RabbitMQ.Client.Events;
43+
using RabbitMQ.Client.Impl;
4244

4345
namespace RabbitMQ.Client
4446
{
@@ -60,6 +62,8 @@ public class DefaultBasicConsumer : IBasicConsumer
6062
private string m_consumerTag = null;
6163
private bool m_running = false;
6264
private ShutdownEventArgs m_shutdownReason = null;
65+
public readonly object m_eventLock = new object();
66+
public ConsumerCancelledEventHandler m_consumerCancelled;
6367

6468
///<summary>Retrieve the IModel instance this consumer is
6569
///registered with.</summary>
@@ -113,6 +117,18 @@ public DefaultBasicConsumer(IModel model)
113117
public virtual void OnCancel()
114118
{
115119
m_running = false;
120+
ConsumerCancelledEventHandler handler;
121+
lock (m_eventLock)
122+
{
123+
handler = m_consumerCancelled;
124+
}
125+
if (handler != null)
126+
{
127+
foreach (ConsumerCancelledEventHandler h in handler.GetInvocationList())
128+
{
129+
h(this, new ConsumerEventArgs(m_consumerTag));
130+
}
131+
}
116132
}
117133

118134

@@ -145,6 +161,24 @@ public virtual void HandleModelShutdown(IModel model, ShutdownEventArgs reason)
145161
OnCancel();
146162
}
147163

164+
public event ConsumerCancelledEventHandler ConsumerCancelled
165+
{
166+
add
167+
{
168+
lock (m_eventLock)
169+
{
170+
m_consumerCancelled += value;
171+
}
172+
}
173+
remove
174+
{
175+
lock (m_eventLock)
176+
{
177+
m_consumerCancelled -= value;
178+
}
179+
}
180+
}
181+
148182
///<summary>Default implementation - override in subclasses.</summary>
149183
///<remarks>
150184
/// Does nothing with the passed in information. Note that in

projects/client/RabbitMQ.Client/src/client/api/IBasicConsumer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
//---------------------------------------------------------------------------
4040

4141
using System;
42+
using RabbitMQ.Client.Events;
4243

4344
namespace RabbitMQ.Client
4445
{
@@ -92,5 +93,10 @@ void HandleBasicDeliver(string consumerTag,
9293
string routingKey,
9394
IBasicProperties properties,
9495
byte[] body);
96+
97+
98+
99+
///<summary>Signalled when the consumer gets cancelled.</summary>
100+
event ConsumerCancelledEventHandler ConsumerCancelled;
95101
}
96102
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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-2013 GoPivotal, Inc.
8+
//
9+
// Licensed under the Apache License, Version 2.0 (the "License");
10+
// you may not use this file except in compliance with the License.
11+
// You may obtain a copy of the License at
12+
//
13+
// http://www.apache.org/licenses/LICENSE-2.0
14+
//
15+
// Unless required by applicable law or agreed to in writing, software
16+
// distributed under the License is distributed on an "AS IS" BASIS,
17+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
// See the License for the specific language governing permissions and
19+
// limitations under the License.
20+
//---------------------------------------------------------------------------
21+
//
22+
// The MPL v1.1:
23+
//
24+
//---------------------------------------------------------------------------
25+
// The contents of this file are subject to the Mozilla Public License
26+
// Version 1.1 (the "License"); you may not use this file except in
27+
// compliance with the License. You may obtain a copy of the License
28+
// at http://www.mozilla.org/MPL/
29+
//
30+
// Software distributed under the License is distributed on an "AS IS"
31+
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
32+
// the License for the specific language governing rights and
33+
// limitations under the License.
34+
//
35+
// The Original Code is RabbitMQ.
36+
//
37+
// The Initial Developer of the Original Code is GoPivotal, Inc.
38+
// Copyright (c) 2013-2013 GoPivotal, Inc. All rights reserved.
39+
//---------------------------------------------------------------------------
40+
41+
using System;
42+
using System.Collections.Generic;
43+
using System.Text;
44+
45+
namespace RabbitMQ.Client.Events
46+
{
47+
///<summary>Delegate used to process consumer cancellations.
48+
///</summary>
49+
public delegate void ConsumerCancelledEventHandler(object sender, ConsumerEventArgs args);
50+
}

0 commit comments

Comments
 (0)