Пример #1
0
        private async Task SendMessageToDeadLetterQueueAsync(MessageExecutionContext messageExecutionContext,
                                                             int retrycount)
        {
            IntegrationEvent <JObject> integrationEvent = null;

            if (!string.IsNullOrEmpty(messageExecutionContext.SerializedMessage))
            {
                integrationEvent = JsonConvert.DeserializeObject <IntegrationEvent <JObject> >(messageExecutionContext.SerializedMessage);
            }

            var deadLetterEventDescriptor =
                new DeadLetterEventDescriptor <JObject>
            {
                CountOfAttempts  = retrycount,
                LastAttemptError = messageExecutionContext.Exception.ToString(),
                Original         = integrationEvent
            };

            messageExecutionContext.DeadLetterIntegrationEvent = new IntegrationEvent <DeadLetterEventDescriptor <JObject> >
            {
                EventType = _modelNamingConventionController.GetDeadLetterQueueMessageEventType(
                    integrationEvent?.EventType),
                Content = deadLetterEventDescriptor
            };

            await _failedExecutionHandler(messageExecutionContext);
        }
Пример #2
0
        private async Task TrySendToRetryQueue(MessageExecutionContext messageExecutionContext)
        {
            if (!messageExecutionContext.MessageProperties.HeadersPresent)
            {
                messageExecutionContext.MessageProperties.Headers = new Dictionary <string, object>();
            }
            int retrycount;

            if (messageExecutionContext.MessageProperties.Headers.ContainsKey("retrycount"))
            {
                retrycount = int.Parse(messageExecutionContext.MessageProperties.Headers["retrycount"].ToString());
            }
            else
            {
                retrycount = 0;
            }

            if (retrycount < _maxRetry)
            {
                _logger.Warning(messageExecutionContext.Exception,
                                $"Failed to process the event({retrycount}/{_maxRetry}): {messageExecutionContext.SerializedMessage}");
                messageExecutionContext.MessageProperties.Headers["retrycount"] = retrycount + 1;
                await _waitEasyNetQPublisher.PublishEventAsync(messageExecutionContext.BodyBytes, messageExecutionContext.MessageProperties,
                                                               _modelNamingConventionController.GetRetryRoutingKey(_name, retrycount + 1));
            }
            else
            {
                _logger.Error(messageExecutionContext.Exception, $"Failed to process the event({retrycount}/{_maxRetry}): {messageExecutionContext.SerializedMessage}");
                await SendMessageToDeadLetterQueueAsync(messageExecutionContext, retrycount);
            }
        }
Пример #3
0
        private async Task HandleDeadletterQueueAndTrackingAsync(MessageExecutionContext messageExecutionContext)
        {
            await HandleDeadletterQueueWithoutTrackingAsync(messageExecutionContext);

            await _publisher.PublishEventAsync(
                messageExecutionContext.DeadLetterIntegrationEvent,
                _modelNamingConventionController.GetDeadLetterQueueRoutingKey(_name));
        }
Пример #4
0
        private async Task HandleDeadletterQueueWithoutTrackingAsync(MessageExecutionContext messageExecutionContext)
        {
            if (_storedeadletter)
            {
                await _waitEasyNetQPublisher.PublishEventAsync(
                    messageExecutionContext.DeadLetterIntegrationEvent,
                    _modelNamingConventionController.GetDeadLetterQueueRoutingKey(_name), true);
            }

            await messageExecutionContext.Subscription.NotifyAboutDeadLetterAsync(messageExecutionContext.SerializedMessage,
                                                                                  messageExecutionContext.Exception);
        }
Пример #5
0
        public async Task HandleErrorAsync(MessageExecutionContext messageExecutionContext)
        {
            switch (messageExecutionContext.Exception)
            {
            case MalformedEventException ex:
                throw ex;

            case DiscardEventException ex:
                throw ex;

            default:
                await TrySendToRetryQueue(messageExecutionContext);

                break;
            }
        }
Пример #6
0
        public void Subscribe(IDictionary <string, ISubscription> subscriptions)
        {
            lock (_threadguard)
            {
                if (_alreadyDisposed)
                {
                    throw new
                          ObjectDisposedException("EasyNetQSubscriber",
                                                  "Called Subscribe Method on Disposed object");
                }

                if (_subscribed == false)
                {
                    _subscriptions      = new ConcurrentDictionary <string, ISubscription>(subscriptions);
                    _consumerDisposable = _bus.Consume(_queue, async(body, properties, info) =>
                    {
                        MessageExecutionContext messageExecutionContext = new MessageExecutionContext(body, properties, info);
                        try
                        {
                            messageExecutionContext.IntergrationEventParsingResult = DefaultIntergrationEventParser.Parse(body);
                            if (messageExecutionContext.IntergrationEventParsingResult.Success)
                            {
                                using (LogContext.PushProperty("CorrelationId", messageExecutionContext.IntergrationEventParsingResult.IntegrationEvent.CorrelationId))
                                {
                                    messageExecutionContext.Subscription = _subscriptionSelector.Select(_subscriptions,
                                                                                                        messageExecutionContext.IntergrationEventParsingResult.IntegrationEvent.EventType);

                                    if (messageExecutionContext.Subscription != null)
                                    {
                                        messageExecutionContext.SerializedMessage = Encoding.UTF8.GetString(body);
                                        _logger.Debug("Subscriber received message: {@body}", messageExecutionContext.SerializedMessage);
                                        await messageExecutionContext.Subscription.InvokeAsync(messageExecutionContext.SerializedMessage);
                                        await _successHandlingStrategy.HandleSuccessAsync(
                                            messageExecutionContext.IntergrationEventParsingResult.IntegrationEvent);
                                    }
                                    else
                                    {
                                        throw new DiscardEventException(
                                            $"The consumer for {messageExecutionContext.IntergrationEventParsingResult.IntegrationEvent.EventType} event type is not matched");
                                    }
                                }
                            }
                            else
                            {
                                throw new MalformedEventException(messageExecutionContext.IntergrationEventParsingResult.Errors);
                            }
                        }
                        catch (Exception e)
                        {
                            messageExecutionContext.Exception = e;
                            await _errorHandlingStrategy.HandleErrorAsync(messageExecutionContext);
                        }
                    }, configuration => configuration.WithPrefetchCount(_prefetchcount));
                    _subscribed = true;
                }
                else
                {
                    foreach (var subscription in subscriptions)
                    {
                        _subscriptions.AddOrUpdate(
                            subscription.Key, subscription.Value,
                            (key, oldValue) => subscription.Value);
                    }
                }
            }
        }