Пример #1
0
 static async Task ProcessMessagesAsync(Message receivedMessage, CancellationToken token)
 {
     try
     {
         ProcessMessage(receivedMessage, "subscr");
         await subscriptionMain.AbandonAsync(receivedMessage.SystemProperties.LockToken);
     }
     catch (Exception)
     {
         await subscriptionMain.AbandonAsync(receivedMessage.SystemProperties.LockToken);
     }
 }
        private async Task OnMessage(Message messageToHandle, CancellationToken lockToken)
        {
            try {
                string groupId = messageToHandle.UserProperties["CollectionId"].ToString();
                if (_messageHolder.TryAdd(groupId, new HashSet <string>()))
                {
                    _processedMessagesHolder.TryAdd(groupId, new ConcurrentDictionary <string, string>());
                }

                string dataJSON = Encoding.UTF8.GetString(messageToHandle.Body);

                int totalMessagesCount = int.Parse(messageToHandle.UserProperties["Count"].ToString());

                string updatedMessage = await ProcessMessage(messageToHandle, dataJSON);

                if (!updatedMessage.Equals("", StringComparison.InvariantCultureIgnoreCase))
                {
                    _processedMessagesHolder[groupId].TryAdd(messageToHandle.MessageId, updatedMessage);
                }

                await subscriptionClient.CompleteAsync(messageToHandle.SystemProperties.LockToken);

                _messageHolder[groupId].Add(dataJSON);

                int processedMessagesCount = _messageHolder[groupId].Count;

                if (processedMessagesCount == totalMessagesCount)
                {
                    // --- Get original messages list
                    HashSet <string> removed = new HashSet <string>();
                    _messageHolder.TryRemove(groupId, out removed);
                    IList <string> messagesList = removed.ToList();

                    // --- Get processed messages list
                    ConcurrentDictionary <string, string> removedDictionary = new ConcurrentDictionary <string, string>();
                    _processedMessagesHolder.TryRemove(groupId, out removedDictionary);
                    IList <string> processedMessagesList = null;
                    if (removedDictionary.Count > 0)
                    {
                        processedMessagesList = removedDictionary.Values.ToList();
                    }
                    if (!(logger is null))
                    {
                        logger.LogInformation(String.Format("====== PROCESSING GROUP OF {0} MESSAGES FOR {1} ======", totalMessagesCount.ToString(), messageToHandle.UserProperties["CollectionId"].ToString()));
                    }
                    await ProcessMessagesWhenLastReceived(messagesList, messageToHandle, processedMessagesList);
                }

                if (!(logger is null))
                {
                    logger.LogInformation(String.Format("----- Processed message {0} of {1} for {2} -----", processedMessagesCount.ToString(), totalMessagesCount.ToString(), messageToHandle.UserProperties["CollectionId"].ToString()));
                }
            } catch (Exception ex) {
                await subscriptionClient.AbandonAsync(messageToHandle.SystemProperties.LockToken);

                //logger.LogError(ex.Message + ex.StackTrace);
                //throw new ApplicationException(ex.Message + ex.StackTrace);
                throw ex;
            }
        }
        async Task ProcessMessagesAsync(Message message, CancellationToken token)
        {
            // Process the message.
            var messageBody    = Encoding.UTF8.GetString(message.Body);
            var sequenceNumber = message.SystemProperties.SequenceNumber;
            var messageId      = message.MessageId;

            if (MessageReceivedCounter == 0) // Set MessageSentTimeStamp on the first message that we send
            {
                MessageReceivedTimeStamp = DateTime.UtcNow;
            }

            MessageReceivedCounter++;

            // Console.WriteLine($"Received message: MessageId:{messageId}, SequenceNumber:{sequenceNumber} Body:{messageBody}");

            var r = _onMessageReceived(messageBody, message.MessageId, sequenceNumber);

            if (r)
            {
                // Complete the message so that it is not received again.
                // This can be done only if the subscriptionClient is created in ReceiveMode.PeekLock mode (which is the default).
                await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
            }
            else
            {
                // Release message so it can be processed again
                await _subscriptionClient.AbandonAsync(message.SystemProperties.LockToken);
            }
            // Note: Use the cancellationToken passed as necessary to determine if the subscriptionClient has already been closed.
            // If subscriptionClient has already been closed, you can choose to not call CompleteAsync() or AbandonAsync() etc.
            // to avoid unnecessary exceptions.
        }
Пример #4
0
        static async Task ProcessMessagesAsync(Message message, CancellationToken token)
        {
            // Process the message.
            Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");

            // This can be done only if the subscriptionClient is created in ReceiveMode.PeekLock mode (which is the default).
            await subscriptionClient.AbandonAsync(message.SystemProperties.LockToken);

            Console.WriteLine(message.SystemProperties.DeliveryCount);
        }
        public async Task <bool> WaitForPause(Message message, CancellationToken cancellationToken)
        {
            if (_pauseUntil > DateTimeOffset.UtcNow)
            {
                await _client.AbandonAsync(message.SystemProperties.LockToken);

                Console.WriteLine("Pausing message handling until we are able to get new secrets.");
                await Task.Delay(_pauseUntil.Subtract(DateTimeOffset.UtcNow), cancellationToken);

                return(true);
            }
            return(false);
        }
Пример #6
0
        private async Task HandleMessageAsync(Message message, CancellationToken cancellationToken)
        {
            var formatter = new BinaryFormatter();

            if (message.Body == null)
            {
                await _subClient.AbandonAsync(message.SystemProperties.LockToken);

                return;
            }

            var objectStream = new MemoryStream(message.Body);
            var updateEvent  = (ContentPageChangeEventArgs)formatter.Deserialize(objectStream);

            if (updateEvent.Title == null)
            {
                _logger.LogWarning($"Changeset {updateEvent.ChangesetId} has null title");
                await _subClient.CompleteAsync(message.SystemProperties.LockToken);

                return;
            }

            var prevMeta = _cache.GetPageMetadata(updateEvent.Title);

            if (prevMeta != null && prevMeta.ChangeSetId > updateEvent.ChangesetId)
            {
                _logger.LogWarning($"Update to page {updateEvent.Title} has higher local rev, {prevMeta.ChangeSetId} > {updateEvent.ChangesetId}");
                await _subClient.CompleteAsync(message.SystemProperties.LockToken);

                return;
            }

            try
            {
                var(meta, page) = await _apiClient.GetPageAsync(updateEvent.Title, updateEvent.ChangesetId);

                if (meta != null && page != null)
                {
                    _cache.PutPageContent(updateEvent.Title, meta, page);
                    await _subClient.CompleteAsync(message.SystemProperties.LockToken);

                    _logger.LogInformation($"Updated page {updateEvent.Title} to rev {updateEvent.ChangesetId}");
                }
            }
            catch (Exception exc)
            {
                _logger.LogError(exc, "MW getting update failed");
            }
        }
Пример #7
0
        public async Task ReceiveMessageFromTopic <T>(Func <T, MessageProcessResponse> onProcess)
        {
            MessageHandlerOptions options = new MessageHandlerOptions(e =>
            {
                Trace.TraceError(e.Exception.Message);
                return(Task.CompletedTask);
            })
            {
                AutoComplete         = false,
                MaxAutoRenewDuration = TimeSpan.FromMinutes(1)
            };

            await Task.Run(() => _subscriptionClient.RegisterMessageHandler(
                               async(message, token) =>
            {
                try
                {
                    // Get message
                    string data = Encoding.UTF8.GetString(message.Body);
                    T item      = JsonConvert.DeserializeObject <T>(data);

                    // Process message
                    MessageProcessResponse result = onProcess(item);

                    switch (result)
                    {
                    case MessageProcessResponse.Complete:
                        await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
                        break;

                    case MessageProcessResponse.Abandon:
                        await _subscriptionClient.AbandonAsync(message.SystemProperties.LockToken);
                        break;

                    case MessageProcessResponse.Dead:
                        await _subscriptionClient.DeadLetterAsync(message.SystemProperties.LockToken);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    await _subscriptionClient.DeadLetterAsync(message.SystemProperties.LockToken);
                    Trace.TraceError(ex.Message);
                }
            }, options));
        }
Пример #8
0
        private async Task MessageHandler(Message message, CancellationToken cancellationToken)
        {
            try
            {
                var payload = Encoding.Default.GetString(message.Body);
                var evt     = JsonConvert.DeserializeObject <LifeCycleEvent>(
                    payload, _serializerSettings);

                NotifySubscribers(evt);

                await _subscriptionClient
                .CompleteAsync(message.SystemProperties.LockToken)
                .ConfigureAwait(false);
            }
            catch
            {
                await _subscriptionClient.AbandonAsync(message.SystemProperties.LockToken);
            }
        }
Пример #9
0
        private async Task ProcessMessagesAsync(Message message, CancellationToken cancellationToken)
        {
            // Process the message.
            await _messageHandler(message, cancellationToken);


            // The cancellationToken is used to determine if the subscriptionClient has already been closed.
            if (cancellationToken.IsCancellationRequested)
            {
                // If subscriptionClient has already been closed, call AbandonAsync() to avoid unnecessary exceptions.
                await _subscriptionClient.AbandonAsync(message.SystemProperties.LockToken);
            }
            else
            {
                // Complete the message so that it is not received again.
                // This can be done only if the subscriptionClient is created in ReceiveMode.PeekLock mode (which is the default).
                await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
            }
        }
 public Task AbandonAsync(string lockToken, IDictionary <string, object> propertiesToModify = null)
 {
     return(_subscriptionClient.AbandonAsync(lockToken, propertiesToModify));
 }
 public override async Task AbandonEvent(string locktoken)
 {
     await _subscriptionClient.AbandonAsync(locktoken);
 }