private async Task DeleteOrReleaseMessage(QueueMessageContext <T> context, QueueMessage queueMessage)
 {
     if (context.Success)
     {
         await queueMessage.Delete();
     }
     else
     {
         await queueMessage.Release(_delay);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Consumes the next message off the queue. Set context.Success to <c>false</c> to *Release* the message back to the queue; otherwise it will be automatically deleted.
        /// </summary>
        /// <param name="consumeAction"></param>
        /// <param name="timeout"></param>
        /// <returns>
        /// Returns <c>false</c> if the queue is empty; otherwise <c>true</c>.
        /// </returns>
        public bool Consume(Action <QueueMessageContext <T>, T> consumeAction, int?timeout = null)
        {
            QueueMessage queueMessage = Next(timeout);

            if (queueMessage == null)
            {
                return(false);
            }

            var context = new QueueMessageContext <T>
            {
                Message = queueMessage,
                Success = true,
                Client  = this
            };

            try
            {
                consumeAction(context, queueMessage.ReadValueAs <T>());
            }
            catch (Exception ex)
            {
                if (_errorHandler != null)
                {
                    context.Success = false;
                    _errorHandler(context, ex);
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                if (context.Success)
                {
                    queueMessage.Delete();
                }
                else
                {
                    queueMessage.Release(_delay);
                }
            }

            return(true);
        }