@@ -21,28 +21,18 @@ private enum PauseStatus
21
21
PAUSED ,
22
22
}
23
23
24
- private readonly AmqpConnection _connection ;
25
- private readonly string _address ;
26
- private readonly MessageHandler _messageHandler ;
27
- private readonly int _initialCredits ;
28
- private readonly Map _filters ;
29
24
private readonly Guid _id = Guid . NewGuid ( ) ;
30
25
31
26
private ReceiverLink ? _receiverLink ;
32
27
33
28
private PauseStatus _pauseStatus = PauseStatus . UNPAUSED ;
34
29
private readonly UnsettledMessageCounter _unsettledMessageCounter = new ( ) ;
30
+ private readonly ConsumerConfiguration _configuration ;
35
31
36
- public AmqpConsumer ( AmqpConnection connection , string address ,
37
- MessageHandler messageHandler , int initialCredits , Map filters )
32
+ public AmqpConsumer ( ConsumerConfiguration configuration )
38
33
{
39
- _connection = connection ;
40
- _address = address ;
41
- _messageHandler = messageHandler ;
42
- _initialCredits = initialCredits ;
43
- _filters = filters ;
44
-
45
- if ( false == _connection . Consumers . TryAdd ( _id , this ) )
34
+ _configuration = configuration ;
35
+ if ( false == _configuration . Connection . Consumers . TryAdd ( _id , this ) )
46
36
{
47
37
// TODO error?
48
38
}
@@ -52,9 +42,20 @@ public override async Task OpenAsync()
52
42
{
53
43
try
54
44
{
55
- TaskCompletionSource < ReceiverLink > attachCompletedTcs = new ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
45
+ TaskCompletionSource < ReceiverLink > attachCompletedTcs =
46
+ new ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
47
+
48
+ // this is an event to get the filters to the listener context
49
+ // it _must_ be here because in case of reconnect the original filters could be not valid anymore
50
+ // so the function must be called every time the consumer is opened normally or by reconnection
51
+ // if ListenerContext is null the function will do nothing
52
+ // ListenerContext will override only the filters the selected filters.
53
+ _configuration . ListenerContext ? . Invoke (
54
+ new IConsumerBuilder . ListenerContext ( new ListenerStreamOptions ( _configuration . Filters ) ) ) ;
56
55
57
- Attach attach = Utils . CreateAttach ( _address , DeliveryMode . AtLeastOnce , _id , _filters ) ;
56
+
57
+ Attach attach = Utils . CreateAttach ( _configuration . Address , DeliveryMode . AtLeastOnce , _id ,
58
+ _configuration . Filters ) ;
58
59
59
60
void onAttached ( ILink argLink , Attach argAttach )
60
61
{
@@ -74,7 +75,7 @@ void onAttached(ILink argLink, Attach argAttach)
74
75
ReceiverLink ? tmpReceiverLink = null ;
75
76
Task receiverLinkTask = Task . Run ( async ( ) =>
76
77
{
77
- Session session = await _connection . _nativePubSubSessions . GetOrCreateSessionAsync ( )
78
+ Session session = await _configuration . Connection . _nativePubSubSessions . GetOrCreateSessionAsync ( )
78
79
. ConfigureAwait ( false ) ;
79
80
tmpReceiverLink = new ReceiverLink ( session , _id . ToString ( ) , attach , onAttached ) ;
80
81
} ) ;
@@ -89,7 +90,7 @@ await receiverLinkTask.WaitAsync(waitSpan)
89
90
. ConfigureAwait ( false ) ;
90
91
91
92
System . Diagnostics . Debug . Assert ( tmpReceiverLink != null ) ;
92
- System . Diagnostics . Debug . Assert ( Object . ReferenceEquals ( _receiverLink , tmpReceiverLink ) ) ;
93
+ System . Diagnostics . Debug . Assert ( object . ReferenceEquals ( _receiverLink , tmpReceiverLink ) ) ;
93
94
94
95
if ( _receiverLink is null )
95
96
{
@@ -103,7 +104,7 @@ await receiverLinkTask.WaitAsync(waitSpan)
103
104
}
104
105
else
105
106
{
106
- _receiverLink . SetCredit ( _initialCredits ) ;
107
+ _receiverLink . SetCredit ( _configuration . InitialCredits ) ;
107
108
108
109
// TODO save / cancel task
109
110
_ = Task . Run ( ProcessMessages ) ;
@@ -150,7 +151,10 @@ private async Task ProcessMessages()
150
151
151
152
// TODO catch exceptions thrown by handlers,
152
153
// then call exception handler?
153
- await _messageHandler ( context , amqpMessage ) . ConfigureAwait ( false ) ;
154
+ if ( _configuration . Handler != null )
155
+ {
156
+ await _configuration . Handler ( context , amqpMessage ) . ConfigureAwait ( false ) ;
157
+ }
154
158
}
155
159
}
156
160
catch ( Exception e )
@@ -173,20 +177,24 @@ public void Pause()
173
177
if ( _receiverLink is null )
174
178
{
175
179
// TODO create "internal bug" exception type?
176
- throw new InvalidOperationException ( "_receiverLink is null, report via https://github.com/rabbitmq/rabbitmq-amqp-dotnet-client/issues" ) ;
180
+ throw new InvalidOperationException (
181
+ "_receiverLink is null, report via https://github.com/rabbitmq/rabbitmq-amqp-dotnet-client/issues" ) ;
177
182
}
178
183
179
- if ( ( int ) PauseStatus . UNPAUSED == Interlocked . CompareExchange ( ref Unsafe . As < PauseStatus , int > ( ref _pauseStatus ) ,
180
- ( int ) PauseStatus . PAUSING , ( int ) PauseStatus . UNPAUSED ) )
184
+ if ( ( int ) PauseStatus . UNPAUSED == Interlocked . CompareExchange (
185
+ ref Unsafe . As < PauseStatus , int > ( ref _pauseStatus ) ,
186
+ ( int ) PauseStatus . PAUSING , ( int ) PauseStatus . UNPAUSED ) )
181
187
{
182
188
_receiverLink . SetCredit ( credit : 0 ) ;
183
189
184
- if ( ( int ) PauseStatus . PAUSING != Interlocked . CompareExchange ( ref Unsafe . As < PauseStatus , int > ( ref _pauseStatus ) ,
185
- ( int ) PauseStatus . PAUSED , ( int ) PauseStatus . PAUSING ) )
190
+ if ( ( int ) PauseStatus . PAUSING != Interlocked . CompareExchange (
191
+ ref Unsafe . As < PauseStatus , int > ( ref _pauseStatus ) ,
192
+ ( int ) PauseStatus . PAUSED , ( int ) PauseStatus . PAUSING ) )
186
193
{
187
194
_pauseStatus = PauseStatus . UNPAUSED ;
188
195
// TODO create "internal bug" exception type?
189
- throw new InvalidOperationException ( "error transitioning from PAUSING -> PAUSED, report via https://github.com/rabbitmq/rabbitmq-amqp-dotnet-client/issues" ) ;
196
+ throw new InvalidOperationException (
197
+ "error transitioning from PAUSING -> PAUSED, report via https://github.com/rabbitmq/rabbitmq-amqp-dotnet-client/issues" ) ;
190
198
}
191
199
}
192
200
else
@@ -197,24 +205,23 @@ public void Pause()
197
205
198
206
public long UnsettledMessageCount
199
207
{
200
- get
201
- {
202
- return _unsettledMessageCounter . Get ( ) ;
203
- }
208
+ get { return _unsettledMessageCounter . Get ( ) ; }
204
209
}
205
210
206
211
public void Unpause ( )
207
212
{
208
213
if ( _receiverLink is null )
209
214
{
210
215
// TODO create "internal bug" exception type?
211
- throw new InvalidOperationException ( "_receiverLink is null, report via https://github.com/rabbitmq/rabbitmq-amqp-dotnet-client/issues" ) ;
216
+ throw new InvalidOperationException (
217
+ "_receiverLink is null, report via https://github.com/rabbitmq/rabbitmq-amqp-dotnet-client/issues" ) ;
212
218
}
213
219
214
- if ( ( int ) PauseStatus . PAUSED == Interlocked . CompareExchange ( ref Unsafe . As < PauseStatus , int > ( ref _pauseStatus ) ,
215
- ( int ) PauseStatus . UNPAUSED , ( int ) PauseStatus . PAUSED ) )
220
+ if ( ( int ) PauseStatus . PAUSED == Interlocked . CompareExchange (
221
+ ref Unsafe . As < PauseStatus , int > ( ref _pauseStatus ) ,
222
+ ( int ) PauseStatus . UNPAUSED , ( int ) PauseStatus . PAUSED ) )
216
223
{
217
- _receiverLink . SetCredit ( credit : _initialCredits ) ;
224
+ _receiverLink . SetCredit ( credit : _configuration . InitialCredits ) ;
218
225
}
219
226
else
220
227
{
@@ -240,19 +247,20 @@ await _receiverLink.CloseAsync(TimeSpan.FromSeconds(5))
240
247
}
241
248
catch ( Exception ex )
242
249
{
243
- Trace . WriteLine ( TraceLevel . Warning , "Failed to close receiver link. The consumer will be closed anyway" , ex ) ;
250
+ Trace . WriteLine ( TraceLevel . Warning , "Failed to close receiver link. The consumer will be closed anyway" ,
251
+ ex ) ;
244
252
}
245
253
246
254
_receiverLink = null ;
247
255
OnNewStatus ( State . Closed , null ) ;
248
- _connection . Consumers . TryRemove ( _id , out _ ) ;
256
+ _configuration . Connection . Consumers . TryRemove ( _id , out _ ) ;
249
257
}
250
258
251
259
public override string ToString ( )
252
260
{
253
- return $ "Consumer{{Address='{ _address } ', " +
261
+ return $ "Consumer{{Address='{ _configuration . Address } ', " +
254
262
$ "id={ _id } , " +
255
- $ "Connection='{ _connection } ', " +
263
+ $ "Connection='{ _configuration . Connection } ', " +
256
264
$ "State='{ State } '}}";
257
265
}
258
266
}
0 commit comments