Exemplo n.º 1
0
        private void MessageReceived(BasicDeliverEventArgs basicDeliverEventArgs, IModel channel)
        {
            var tag = basicDeliverEventArgs.DeliveryTag;
            var ma  = new MessageAcceptor(channel, tag);

            try
            {
                var model = _messageDeserializer.Deserialize(basicDeliverEventArgs.Body);

                try
                {
                    _middlewareQueue.RunMiddlewaresAsync(
                        basicDeliverEventArgs,
                        model,
                        ma,
                        _cancellationTokenSource.Token)
                    .GetAwaiter().GetResult();
                }
                catch (OperationCanceledException)
                {
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, _settings.GetSubscriberName());

                ma.Reject();
            }
        }
Exemplo n.º 2
0
 private void ExecuteStrategy(TTopicModel model, MessageAcceptor ma)
 {
     _errorHandlingStrategy.Execute(
         _cancallableEventHandler != null
         ? () => _cancallableEventHandler(model, _cancellationTokenSource.Token).GetAwaiter().GetResult()
         : (Action)(() => _eventHandler(model).GetAwaiter().GetResult()),
         ma,
         _cancellationTokenSource.Token);
 }
Exemplo n.º 3
0
        private void MessageReceived(BasicDeliverEventArgs basicDeliverEventArgs, IModel channel)
        {
            var tag = basicDeliverEventArgs.DeliveryTag;
            var ma  = new MessageAcceptor(channel, tag);

            try
            {
                var body = basicDeliverEventArgs.Body;

                if (_enableMessageDeduplication)
                {
                    var deduplicationHeaderBytes = string.IsNullOrEmpty(_deduplicatorHeader) ||
                                                   !basicDeliverEventArgs.BasicProperties.Headers.ContainsKey(_deduplicatorHeader)
                        ? Array.Empty <byte>()
                        : Encoding.UTF8.GetBytes(basicDeliverEventArgs.BasicProperties.Headers[_deduplicatorHeader].ToJson());
                    var isDuplicated = deduplicationHeaderBytes.Length == 0
                        ? !_deduplicator.EnsureNotDuplicateAsync(body).GetAwaiter().GetResult()
                        : !_deduplicator.EnsureNotDuplicateAsync(deduplicationHeaderBytes).GetAwaiter().GetResult();

                    if (isDuplicated)
                    {
                        ma.Accept();
                        return;
                    }
                }

                var model = _messageDeserializer.Deserialize(body);

                if (_submitTelemetry)
                {
                    var telemetryOperation = InitTelemetryOperation(body.Length);
                    try
                    {
                        ExecuteStrategy(model, ma);
                    }
                    catch (Exception e)
                    {
                        telemetryOperation.Telemetry.Success = false;
                        _telemetry.TrackException(e);
                        if (!(e is OperationCanceledException))
                        {
                            throw;
                        }
                    }
                    finally
                    {
                        _telemetry.StopOperation(telemetryOperation);
                    }
                }
                else
                {
                    try
                    {
                        ExecuteStrategy(model, ma);
                    }
                    catch (OperationCanceledException)
                    {
                    }
                }
            }
            catch (Exception ex)
            {
                _log.WriteError(nameof(MessageReceived), _settings.GetSubscriberName(), ex);

                ma.Reject();
            }
        }