Beispiel #1
0
        public void PublishOnRetryExchange(Channel channel, T message, RetryInformations retryInformations)
        {
            List <int> delays = _messageBrokerConfiguration.DelaysInMsBetweenEachRetry;
            int        delay  = 0;

            if (delays.Count >= retryInformations.NumberOfRetry)
            {
                delay = delays[retryInformations.NumberOfRetry];
            }
            else
            {
                delay = delays.Last();
            }
            retryInformations.NumberOfRetry++;
            RetryMessage <T> retryMessage = new RetryMessage <T>
            {
                OriginalMessage   = message,
                RetryInformations = retryInformations
            };

            string bindingValue = $"{channel.Value}.{delay}";

            using (var publisher = _messagingService.GetPublisher($"{_messageBrokerConfiguration.BusName}.{RetryStrategyConfiguration.RetryExchangeSuffix}", true))
            {
                TryPublishWithBrokerAcknowledgement(publisher, bindingValue, JsonConvert.SerializeObject(retryMessage));
            }
        }
Beispiel #2
0
        public MessageInformations ApplyRetryStrategy(Channel channel, T deliveredMessage)
        {
            if (String.IsNullOrEmpty(deliveredMessage.DeliveryTag))
            {
                throw new ArgumentNullException(nameof(deliveredMessage.DeliveryTag));
            }
            if (!RetryInformations.ContainsKey(deliveredMessage.DeliveryTag))
            {
                throw new MerqureToolsException($"unknown delivery tag {deliveredMessage.DeliveryTag}");
            }

            RetryInformations retryInformations = RetryInformations[deliveredMessage.DeliveryTag];
            var messageInformations             = new MessageInformations();

            if (IsGoingToErrorExchange(retryInformations))
            {
                _producer.PublishOnErrorExchange(channel, deliveredMessage, retryInformations);
                messageInformations.IsOnErrorBus = true;
            }
            else
            {
                _producer.PublishOnRetryExchange(channel, deliveredMessage, retryInformations);
            }

            AcknowlegdeDeliveredMessage(channel, deliveredMessage);
            RetryInformations.Remove(deliveredMessage.DeliveryTag);
            return(messageInformations);
        }
Beispiel #3
0
 private bool IsGoingToErrorExchange(RetryInformations technicalInformations)
 {
     if (!_retryConfiguration.DelaysInMsBetweenEachRetry.Any())
     {
         return(true);
     }
     if (_retryConfiguration.MessageIsGoingIntoErrorBusAfterAllRepeat &&
         technicalInformations.NumberOfRetry == _retryConfiguration.DelaysInMsBetweenEachRetry.Count)
     {
         return(true);
     }
     return(false);
 }
Beispiel #4
0
        internal void PublishOnErrorExchange(Channel channel, T message, RetryInformations technicalInformations)
        {
            string           errorChanel  = $"{channel.Value}.error";
            RetryMessage <T> retryMessage = new RetryMessage <T>
            {
                OriginalMessage   = message,
                RetryInformations = technicalInformations
            };

            using (var publisher = _messagingService.GetPublisher($"{_messageBrokerConfiguration.BusName}.{RetryStrategyConfiguration.ErrorExchangeSuffix}", true))
            {
                TryPublishWithBrokerAcknowledgement(publisher, errorChanel, JsonConvert.SerializeObject(retryMessage));
            }
        }
Beispiel #5
0
        internal void SendToErrorExchange(Channel channel, T deliveredMessage)
        {
            if (String.IsNullOrEmpty(deliveredMessage.DeliveryTag))
            {
                throw new ArgumentNullException(nameof(deliveredMessage.DeliveryTag));
            }
            if (!RetryInformations.ContainsKey(deliveredMessage.DeliveryTag))
            {
                throw new MerqureToolsException($"unknown delivery tag {deliveredMessage.DeliveryTag}");
            }

            RetryInformations retryInformations = RetryInformations[deliveredMessage.DeliveryTag];

            _producer.PublishOnErrorExchange(channel, deliveredMessage, retryInformations);
            AcknowlegdeDeliveredMessage(channel, deliveredMessage);
            RetryInformations.Remove(deliveredMessage.DeliveryTag);
        }
Beispiel #6
0
        public MessageInformations ForceRetryStrategy(Channel channel, T message, int attemptNumber)
        {
            var retryInformations = new RetryInformations()
            {
                NumberOfRetry = attemptNumber > 0 ? attemptNumber - 1 : 0
            };
            var messageInformations = new MessageInformations();

            if (IsGoingToErrorExchange(retryInformations))
            {
                _producer.PublishOnErrorExchange(channel, message, retryInformations);
                messageInformations.IsOnErrorBus = true;
            }
            else
            {
                _producer.PublishOnRetryExchange(channel, message, retryInformations);
            }
            return(messageInformations);
        }