コード例 #1
0
        private void ConnectAndRead(RabbitMqSubscriptionSettings settings)
        {
            var factory = new ConnectionFactory {
                Uri = new Uri(settings.ConnectionString, UriKind.Absolute)
            };

            _log.WriteInfo(nameof(ConnectAndRead), settings.GetSubscriberName(), $"Trying to connect to {factory.Endpoint} ({_exchangeQueueName})");

            var cn = $"[Sub] {PlatformServices.Default.Application.ApplicationName} {PlatformServices.Default.Application.ApplicationVersion} to {_exchangeQueueName}";

            using (var connection = factory.CreateConnection(cn))
                using (var channel = connection.CreateModel())
                {
                    _log.WriteInfo(nameof(ConnectAndRead), settings.GetSubscriberName(), $"Connected to {factory.Endpoint} ({_exchangeQueueName})");

                    if (_prefetchCount.HasValue)
                    {
                        channel.BasicQos(0, _prefetchCount.Value, false);
                    }

                    var queueName = _messageReadStrategy.Configure(settings, channel);

                    var consumer = new QueueingBasicConsumer(channel);
                    var tag      = channel.BasicConsume(queueName, false, consumer);

                    //consumer.Received += MessageReceived;

                    while (!IsStopped())
                    {
                        if (!connection.IsOpen)
                        {
                            throw new RabbitMqBrokerException($"{settings.GetSubscriberName()}: connection to {connection.Endpoint} is closed");
                        }

                        var delivered = consumer.Queue.Dequeue(2000, out var eventArgs);

                        _reconnectionsInARowCount = 0;

                        if (delivered)
                        {
                            MessageReceived(eventArgs, channel);
                        }
                    }

                    channel.BasicCancel(tag);
                    connection.Close();
                }
        }
コード例 #2
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();
            }
        }
コード例 #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();
            }
        }