8
8
9
9
namespace RabbitMQ . Client
10
10
{
11
- public class AsyncDefaultBasicConsumer : IBasicConsumer , IAsyncBasicConsumer
11
+ public class AsyncDefaultBasicConsumer : IAsyncBasicConsumer
12
12
{
13
13
private readonly HashSet < string > _consumerTags = new HashSet < string > ( ) ;
14
14
15
15
/// <summary>
16
- /// Creates a new instance of an <see cref="DefaultBasicConsumer "/>.
16
+ /// Creates a new instance of an <see cref="AsyncDefaultBasicConsumer "/>.
17
17
/// </summary>
18
18
public AsyncDefaultBasicConsumer ( )
19
19
{
@@ -70,28 +70,31 @@ public event AsyncEventHandler<ConsumerEventArgs> ConsumerCancelled
70
70
/// <summary>
71
71
/// Called when the consumer is cancelled for reasons other than by a basicCancel:
72
72
/// e.g. the queue has been deleted (either by this channel or by any other channel).
73
- /// See <see cref="HandleBasicCancelOk "/> for notification of consumer cancellation due to basicCancel
73
+ /// See <see cref="HandleBasicCancelOkAsync "/> for notification of consumer cancellation due to basicCancel
74
74
/// </summary>
75
75
/// <param name="consumerTag">Consumer tag this consumer is registered.</param>
76
- public virtual Task HandleBasicCancel ( string consumerTag )
76
+ /// <param name="cancellationToken">The cancellation token for this operation.</param>
77
+ public virtual Task HandleBasicCancelAsync ( string consumerTag , CancellationToken cancellationToken )
77
78
{
78
- return OnCancel ( consumerTag ) ;
79
+ return OnCancelAsync ( cancellationToken , consumerTag ) ;
79
80
}
80
81
81
82
/// <summary>
82
83
/// Called upon successful deregistration of the consumer from the broker.
83
84
/// </summary>
84
85
/// <param name="consumerTag">Consumer tag this consumer is registered.</param>
85
- public virtual Task HandleBasicCancelOk ( string consumerTag )
86
+ /// <param name="cancellationToken">The cancellation token for this operation.</param>
87
+ public virtual Task HandleBasicCancelOkAsync ( string consumerTag , CancellationToken cancellationToken )
86
88
{
87
- return OnCancel ( consumerTag ) ;
89
+ return OnCancelAsync ( cancellationToken , consumerTag ) ;
88
90
}
89
91
90
92
/// <summary>
91
93
/// Called upon successful registration of the consumer with the broker.
92
94
/// </summary>
93
95
/// <param name="consumerTag">Consumer tag this consumer is registered.</param>
94
- public virtual Task HandleBasicConsumeOk ( string consumerTag )
96
+ /// <param name="cancellationToken">The cancellation token for this operation.</param>
97
+ public virtual Task HandleBasicConsumeOkAsync ( string consumerTag , CancellationToken cancellationToken )
95
98
{
96
99
_consumerTags . Add ( consumerTag ) ;
97
100
IsRunning = true ;
@@ -108,13 +111,14 @@ public virtual Task HandleBasicConsumeOk(string consumerTag)
108
111
/// Accessing the body at a later point is unsafe as its memory can
109
112
/// be already released.
110
113
/// </remarks>
111
- public virtual Task HandleBasicDeliver ( string consumerTag ,
114
+ public virtual Task HandleBasicDeliverAsync ( string consumerTag ,
112
115
ulong deliveryTag ,
113
116
bool redelivered ,
114
117
string exchange ,
115
118
string routingKey ,
116
- in ReadOnlyBasicProperties properties ,
117
- ReadOnlyMemory < byte > body )
119
+ ReadOnlyBasicProperties properties ,
120
+ ReadOnlyMemory < byte > body ,
121
+ CancellationToken cancellationToken )
118
122
{
119
123
// Nothing to do here.
120
124
return Task . CompletedTask ;
@@ -125,63 +129,33 @@ public virtual Task HandleBasicDeliver(string consumerTag,
125
129
/// </summary>
126
130
/// <param name="channel">A channel this consumer was registered on.</param>
127
131
/// <param name="reason">Shutdown context.</param>
128
- public virtual Task HandleChannelShutdown ( object channel , ShutdownEventArgs reason )
132
+ /// <param name="cancellationToken">The cancellation token for this operation.</param>
133
+ public virtual Task HandleChannelShutdownAsync ( object channel , ShutdownEventArgs reason ,
134
+ CancellationToken cancellationToken )
129
135
{
130
136
ShutdownReason = reason ;
131
- return OnCancel ( _consumerTags . ToArray ( ) ) ;
137
+ return OnCancelAsync ( cancellationToken , _consumerTags . ToArray ( ) ) ;
132
138
}
133
139
134
140
/// <summary>
135
141
/// Default implementation - overridable in subclasses.</summary>
136
- /// <param name="consumerTags">The set of consumer tags that where cancelled</param>
137
142
/// <remarks>
143
+ /// <param name="cancellationToken">The cancellation token for this operation.</param>
144
+ /// <param name="consumerTags">The set of consumer tags that where cancelled</param>
138
145
/// This default implementation simply sets the <see cref="IsRunning"/> property to false, and takes no further action.
139
146
/// </remarks>
140
- public virtual async Task OnCancel ( params string [ ] consumerTags )
147
+ public virtual async Task OnCancelAsync ( CancellationToken cancellationToken , params string [ ] consumerTags )
141
148
{
142
149
IsRunning = false ;
143
150
if ( ! _consumerCancelledWrapper . IsEmpty )
144
151
{
145
- // TODO cancellation token
146
- await _consumerCancelledWrapper . InvokeAsync ( this , new ConsumerEventArgs ( consumerTags ) , CancellationToken . None )
152
+ await _consumerCancelledWrapper . InvokeAsync ( this , new ConsumerEventArgs ( consumerTags ) , cancellationToken )
147
153
. ConfigureAwait ( false ) ;
148
154
}
149
155
foreach ( string consumerTag in consumerTags )
150
156
{
151
157
_consumerTags . Remove ( consumerTag ) ;
152
158
}
153
159
}
154
-
155
- event EventHandler < ConsumerEventArgs > IBasicConsumer . ConsumerCancelled
156
- {
157
- add { throw new InvalidOperationException ( "Should never be called. Enable 'DispatchConsumersAsync'." ) ; }
158
- remove { throw new InvalidOperationException ( "Should never be called. Enable 'DispatchConsumersAsync'." ) ; }
159
- }
160
-
161
- void IBasicConsumer . HandleBasicCancelOk ( string consumerTag )
162
- {
163
- throw new InvalidOperationException ( "Should never be called. Enable 'DispatchConsumersAsync'." ) ;
164
- }
165
-
166
- void IBasicConsumer . HandleBasicConsumeOk ( string consumerTag )
167
- {
168
- throw new InvalidOperationException ( "Should never be called. Enable 'DispatchConsumersAsync'." ) ;
169
- }
170
-
171
- Task IBasicConsumer . HandleBasicDeliverAsync ( string consumerTag , ulong deliveryTag , bool redelivered , string exchange , string routingKey ,
172
- ReadOnlyBasicProperties properties , ReadOnlyMemory < byte > body )
173
- {
174
- throw new InvalidOperationException ( "Should never be called. Enable 'DispatchConsumersAsync'." ) ;
175
- }
176
-
177
- void IBasicConsumer . HandleChannelShutdown ( object channel , ShutdownEventArgs reason )
178
- {
179
- throw new InvalidOperationException ( "Should never be called. Enable 'DispatchConsumersAsync'." ) ;
180
- }
181
-
182
- void IBasicConsumer . HandleBasicCancel ( string consumerTag )
183
- {
184
- throw new InvalidOperationException ( "Should never be called. Enable 'DispatchConsumersAsync'." ) ;
185
- }
186
160
}
187
161
}
0 commit comments