1212namespace Azure . Messaging . ServiceBus
1313{
1414 /// <summary>
15- /// Represents a single receiver instance that multiple threads spawned by the
15+ /// Represents a single receiver instance that multiple tasks spawned by the
1616 /// <see cref="ServiceBusProcessor"/> may be using to receive and process messages.
1717 /// The manager will delegate to the user provided callbacks and handle automatic
1818 /// locking of messages.
1919 /// </summary>
2020 internal class ReceiverManager
2121 {
2222 protected virtual ServiceBusReceiver Receiver { get ; set ; }
23- protected readonly ServiceBusConnection _connection ;
24- protected readonly string _fullyQualifiedNamespace ;
25- protected readonly string _entityPath ;
26- protected readonly string _identifier ;
23+
24+ protected readonly ServiceBusProcessor Processor ;
2725 protected readonly TimeSpan ? _maxReceiveWaitTime ;
2826 private readonly ServiceBusReceiverOptions _receiverOptions ;
29- protected readonly ServiceBusProcessorOptions _processorOptions ;
30- private readonly Func < ProcessErrorEventArgs , Task > _errorHandler ;
31- private readonly Func < ProcessMessageEventArgs , Task > _messageHandler ;
27+ protected readonly ServiceBusProcessorOptions ProcessorOptions ;
3228 protected readonly EntityScopeFactory _scopeFactory ;
3329 protected readonly IList < ServiceBusPlugin > _plugins ;
3430
35- protected bool AutoRenewLock => _processorOptions . MaxAutoLockRenewalDuration > TimeSpan . Zero ;
31+ protected bool AutoRenewLock => ProcessorOptions . MaxAutoLockRenewalDuration > TimeSpan . Zero ;
3632
3733 public ReceiverManager (
38- ServiceBusConnection connection ,
39- string fullyQualifiedNamespace ,
40- string entityPath ,
41- string identifier ,
42- ServiceBusProcessorOptions processorOptions ,
43- Func < ProcessMessageEventArgs , Task > messageHandler ,
44- Func < ProcessErrorEventArgs , Task > errorHandler ,
34+ ServiceBusProcessor processor ,
4535 EntityScopeFactory scopeFactory ,
4636 IList < ServiceBusPlugin > plugins )
4737 {
48- _connection = connection ;
49- _fullyQualifiedNamespace = fullyQualifiedNamespace ;
50- _entityPath = entityPath ;
51- _processorOptions = processorOptions ;
38+ Processor = processor ;
39+ ProcessorOptions = processor . Options ;
5240 _receiverOptions = new ServiceBusReceiverOptions
5341 {
54- ReceiveMode = _processorOptions . ReceiveMode ,
55- PrefetchCount = _processorOptions . PrefetchCount ,
42+ ReceiveMode = ProcessorOptions . ReceiveMode ,
43+ PrefetchCount = ProcessorOptions . PrefetchCount ,
5644 } ;
57- _maxReceiveWaitTime = _processorOptions . MaxReceiveWaitTime ;
58- _identifier = identifier ;
45+ _maxReceiveWaitTime = ProcessorOptions . MaxReceiveWaitTime ;
5946 _plugins = plugins ;
6047 Receiver = new ServiceBusReceiver (
61- connection : _connection ,
62- entityPath : _entityPath ,
48+ connection : Processor . Connection ,
49+ entityPath : Processor . EntityPath ,
6350 isSessionEntity : false ,
6451 plugins : _plugins ,
6552 options : _receiverOptions ) ;
66- _errorHandler = errorHandler ;
67- _messageHandler = messageHandler ;
6853 _scopeFactory = scopeFactory ;
6954 }
7055
@@ -121,8 +106,8 @@ await RaiseExceptionReceived(
121106 new ProcessErrorEventArgs (
122107 ex ,
123108 errorSource ,
124- _fullyQualifiedNamespace ,
125- _entityPath ,
109+ Processor . FullyQualifiedNamespace ,
110+ Processor . EntityPath ,
126111 cancellationToken ) )
127112 . ConfigureAwait ( false ) ;
128113 }
@@ -147,12 +132,6 @@ await ProcessOneMessage(
147132 }
148133 }
149134
150- /// <summary>
151- ///
152- /// </summary>
153- /// <param name="message"></param>
154- /// <param name="cancellationToken"></param>
155- /// <returns></returns>
156135 private async Task ProcessOneMessage (
157136 ServiceBusReceivedMessage message ,
158137 CancellationToken cancellationToken )
@@ -177,18 +156,18 @@ private async Task ProcessOneMessage(
177156
178157 try
179158 {
180- ServiceBusEventSource . Log . ProcessorMessageHandlerStart ( _identifier , message . SequenceNumber ) ;
159+ ServiceBusEventSource . Log . ProcessorMessageHandlerStart ( Processor . Identifier , message . SequenceNumber ) ;
181160 await OnMessageHandler ( message , cancellationToken ) . ConfigureAwait ( false ) ;
182- ServiceBusEventSource . Log . ProcessorMessageHandlerComplete ( _identifier , message . SequenceNumber ) ;
161+ ServiceBusEventSource . Log . ProcessorMessageHandlerComplete ( Processor . Identifier , message . SequenceNumber ) ;
183162 }
184163 catch ( Exception ex )
185164 {
186- ServiceBusEventSource . Log . ProcessorMessageHandlerException ( _identifier , message . SequenceNumber , ex . ToString ( ) ) ;
165+ ServiceBusEventSource . Log . ProcessorMessageHandlerException ( Processor . Identifier , message . SequenceNumber , ex . ToString ( ) ) ;
187166 throw ;
188167 }
189168
190169 if ( Receiver . ReceiveMode == ServiceBusReceiveMode . PeekLock &&
191- _processorOptions . AutoCompleteMessages &&
170+ ProcessorOptions . AutoCompleteMessages &&
192171 ! message . IsSettled )
193172 {
194173 errorSource = ServiceBusErrorSource . Complete ;
@@ -214,8 +193,8 @@ await RaiseExceptionReceived(
214193 new ProcessErrorEventArgs (
215194 ex ,
216195 errorSource ,
217- _fullyQualifiedNamespace ,
218- _entityPath ,
196+ Processor . FullyQualifiedNamespace ,
197+ Processor . EntityPath ,
219198 cancellationToken ) )
220199 . ConfigureAwait ( false ) ;
221200
@@ -244,8 +223,8 @@ await RaiseExceptionReceived(
244223 new ProcessErrorEventArgs (
245224 exception ,
246225 ServiceBusErrorSource . Abandon ,
247- _fullyQualifiedNamespace ,
248- _entityPath ,
226+ Processor . FullyQualifiedNamespace ,
227+ Processor . EntityPath ,
249228 cancellationToken ) )
250229 . ConfigureAwait ( false ) ;
251230 }
@@ -264,26 +243,20 @@ protected virtual async Task OnMessageHandler(ServiceBusReceivedMessage message,
264243 message ,
265244 Receiver ,
266245 processorCancellationToken ) ;
267- await _messageHandler ( args ) . ConfigureAwait ( false ) ;
246+ await Processor . OnProcessMessageAsync ( args ) . ConfigureAwait ( false ) ;
268247 }
269248
270- /// <summary>
271- ///
272- /// </summary>
273- /// <param name="message"></param>
274- /// <param name="cancellationTokenSource"></param>
275- /// <returns></returns>
276249 private async Task RenewMessageLock (
277250 ServiceBusReceivedMessage message ,
278251 CancellationTokenSource cancellationTokenSource )
279252 {
280- cancellationTokenSource . CancelAfter ( _processorOptions . MaxAutoLockRenewalDuration ) ;
253+ cancellationTokenSource . CancelAfter ( ProcessorOptions . MaxAutoLockRenewalDuration ) ;
281254 CancellationToken cancellationToken = cancellationTokenSource . Token ;
282255 while ( ! cancellationToken . IsCancellationRequested )
283256 {
284257 try
285258 {
286- ServiceBusEventSource . Log . ProcessorRenewMessageLockStart ( _identifier , 1 , message . LockToken ) ;
259+ ServiceBusEventSource . Log . ProcessorRenewMessageLockStart ( Processor . Identifier , 1 , message . LockToken ) ;
287260 TimeSpan delay = CalculateRenewDelay ( message . LockedUntil ) ;
288261
289262 // We're awaiting the task created by 'ContinueWith' to avoid awaiting the Delay task which may be canceled
@@ -300,11 +273,11 @@ private async Task RenewMessageLock(
300273 }
301274
302275 await Receiver . RenewMessageLockAsync ( message , cancellationToken ) . ConfigureAwait ( false ) ;
303- ServiceBusEventSource . Log . ProcessorRenewMessageLockComplete ( _identifier ) ;
276+ ServiceBusEventSource . Log . ProcessorRenewMessageLockComplete ( Processor . Identifier ) ;
304277 }
305278 catch ( Exception ex ) when ( ! ( ex is TaskCanceledException ) )
306279 {
307- ServiceBusEventSource . Log . ProcessorRenewMessageLockException ( _identifier , ex . ToString ( ) ) ;
280+ ServiceBusEventSource . Log . ProcessorRenewMessageLockException ( Processor . Identifier , ex . ToString ( ) ) ;
308281 await HandleRenewLockException ( ex , cancellationToken ) . ConfigureAwait ( false ) ;
309282
310283 // if the error was not transient, break out of the loop
@@ -363,22 +336,17 @@ await RaiseExceptionReceived(
363336 new ProcessErrorEventArgs (
364337 ex ,
365338 ServiceBusErrorSource . RenewLock ,
366- _fullyQualifiedNamespace ,
367- _entityPath ,
339+ Processor . FullyQualifiedNamespace ,
340+ Processor . EntityPath ,
368341 cancellationToken ) ) . ConfigureAwait ( false ) ;
369342 }
370343 }
371344
372- /// <summary>
373- ///
374- /// </summary>
375- /// <param name="eventArgs"></param>
376- /// <returns></returns>
377345 protected async Task RaiseExceptionReceived ( ProcessErrorEventArgs eventArgs )
378346 {
379347 try
380348 {
381- await _errorHandler ( eventArgs ) . ConfigureAwait ( false ) ;
349+ await Processor . OnProcessErrorAsync ( eventArgs ) . ConfigureAwait ( false ) ;
382350 }
383351 catch ( Exception exception )
384352 {
@@ -387,11 +355,6 @@ protected async Task RaiseExceptionReceived(ProcessErrorEventArgs eventArgs)
387355 }
388356 }
389357
390- /// <summary>
391- ///
392- /// </summary>
393- /// <param name="lockedUntil"></param>
394- /// <returns></returns>
395358 protected static TimeSpan CalculateRenewDelay ( DateTimeOffset lockedUntil )
396359 {
397360 var remainingTime = lockedUntil - DateTimeOffset . UtcNow ;
0 commit comments