private void ProcessMessages() { int retryAttempt = 0; var policyFalse = Policy.HandleResult <bool>(false).WaitAndRetryForever((r) => TimeSpan.FromSeconds(Math.Min(8, Math.Pow(2, ++retryAttempt)))); var policyTrue = Policy.HandleResult <bool>(true).RetryForever((result) => retryAttempt = 0); var policy = Policy.Wrap(policyFalse, policyTrue); policy.Execute((cancellationToken) => { receivedHandled.WaitOne(); foreach (QueuePriority priority in Enum.GetValues(typeof(QueuePriority)).Cast <QueuePriority>().OrderByDescending(p => p)) { var queue = GetAzureQueue(priority, false); if (this.messageReceivedHandler != null) { var message = queue?.GetMessage(this.VisibilityTimeout); if (message != null) { var eventArgs = new MessageReceivedEventArgs { MessageWrapper = new MessageWrapper(message, queue) }; OnMessageReceived(eventArgs); return(true); } } if (this.messagesReceivedHandler != null) { var messages = queue?.GetMessages(DequeueCount, this.VisibilityTimeout); if (messages.Any()) { var eventArgs = new MessagesReceivedEventArgs { MessageWrappers = messages.Select(m => new MessageWrapper(m, queue)) }; OnMessagesReceived(eventArgs); return(true); } } } return(false); }, this.cancellationToken.Token); }
protected virtual void OnMessagesReceived(MessagesReceivedEventArgs e) { this.messagesReceivedHandler?.Invoke(this, e); }