private void ConsumeMessage(SubscriberInfo subscriberInfo, IBrokeredMessage brokeredMessage) { try { var messageBody = brokeredMessage.GetBody <string>(); var message = JsonConvert.DeserializeObject <TMessage>(messageBody); _logService.Debug("Consuming message id '{0}', topic/subscription '{1}', body '{2}'", brokeredMessage.MessageId, subscriberInfo.SubscriptionPath, messageBody); var state = subscriberInfo.Subscriber.Consume(message); HandleMessageResult(subscriberInfo, state, brokeredMessage, messageBody); _logService.Debug("Consumed message id '{0}', topic/subscription '{1}', body '{2}'", brokeredMessage.MessageId, subscriberInfo.SubscriptionPath, messageBody); } catch (Exception e) { var deadLetterReason = string.Format( "Unexpected exception consuming message id '{0}, topic/subscription '{1}': message will be dead-lettered", brokeredMessage.MessageId, subscriberInfo.SubscriptionPath); _logService.Error(deadLetterReason, e); brokeredMessage.DeadLetter(); } }
private void HandleMessageResult(SubscriberInfo subscriberInfo, ServiceBusMessageStates state, IBrokeredMessage brokeredMessage, string messageBody) { _logService.Debug("Handling message id '{0}', topic/subscription '{1}', state '{2}', body '{3}' ", brokeredMessage.MessageId, subscriberInfo.SubscriptionPath, state, messageBody); switch (state) { case ServiceBusMessageStates.Complete: brokeredMessage.Complete(); break; case ServiceBusMessageStates.Abandon: brokeredMessage.Abandon(); break; case ServiceBusMessageStates.DeadLetter: brokeredMessage.DeadLetter(); break; case ServiceBusMessageStates.Requeue: var scheduledEnqueueTimeUtc = brokeredMessage.ScheduledEnqueueTimeUtc == DateTime.MinValue ? DateTime.UtcNow.AddSeconds(30) : GetDefaultRequeueDateTimeUtc(); var newBrokeredMessage = new BrokeredMessage(messageBody) { ScheduledEnqueueTimeUtc = scheduledEnqueueTimeUtc }; subscriberInfo.TopicClient.Send(newBrokeredMessage); brokeredMessage.Complete(); break; default: var deadLetterReason = string.Format( "Invalid message state '{0}' for message id '{1}', topic/subscription '{2}': message will be dead-lettered", state, brokeredMessage.MessageId, subscriberInfo.SubscriptionPath); _logService.Error(deadLetterReason); brokeredMessage.DeadLetter(); break; } _logService.Debug("Handled message id '{0}', topic/subscription '{1}', state '{2}', body '{3}' ", brokeredMessage.MessageId, subscriberInfo.SubscriptionPath, state, messageBody); }
static bool SafeDeadLetter(IBrokeredMessage msg, string reason) { try { // Mark brokered message as complete. msg.DeadLetter(reason, "Max retries Exceeded."); // Return a result indicating that the message has been completed successfully. return(true); } catch (MessageLockLostException) { // It's too late to compensate the loss of a message lock. We should just ignore it so that it does not break the receive loop. // We should be prepared to receive the same message again. } catch (MessagingException) { // There is nothing we can do as the connection may have been lost, or the underlying topic/subscription may have been removed. // If Complete() fails with this exception, the only recourse is to prepare to receive another message (possibly the same one). } return(false); }
static bool SafeDeadLetter(IBrokeredMessage msg, string reason) { try { // Mark brokered message as complete. msg.DeadLetter(reason, "Max retries Exceeded."); // Return a result indicating that the message has been completed successfully. return true; } catch (MessageLockLostException) { // It's too late to compensate the loss of a message lock. We should just ignore it so that it does not break the receive loop. // We should be prepared to receive the same message again. } catch (MessagingException) { // There is nothing we can do as the connection may have been lost, or the underlying topic/subscription may have been removed. // If Complete() fails with this exception, the only recourse is to prepare to receive another message (possibly the same one). } return false; }