Ejemplo n.º 1
0
        private IModel GetChannel()
        {
            if (_channel != null)
            {
                return(_channel);
            }

            _channel = _connection.CreateModel();
            return(_channel);
        }
Ejemplo n.º 2
0
        public void Start()
        {
            if (!_connection.IsConnected)
            {
                _connection.TryConnect();
            }

            _consumerChannel = _connection.CreateModel();
            _consumerChannel.ExchangeDeclare(SubscriberOptions.Exchange,
                                             SubscriberOptions.ExchangeType, true, false);
            _consumerChannel.QueueDeclare(SubscriberOptions.QueueName, true, false, false, null);
            _consumerChannel.QueueBind(SubscriberOptions.QueueName,
                                       SubscriberOptions.Exchange,
                                       SubscriberOptions.RoutingKey);
            var consumer = new EventingBasicConsumer(_consumerChannel);

            consumer.Received += (model, ea) =>
            {
                var message = Encoding.UTF8.GetString(ea.Body);
                var syncMsg = JsonConvert.DeserializeObject <SyncRequest>(message);
                try
                {
                    Received?.Invoke(this, syncMsg);
                    if (_logger.IsEnabled(LogLevel.Debug))
                    {
                        _logger.LogDebug(
                            $"Received Invoke -> Id:[{syncMsg.Id}],Scope:[{syncMsg.Scope}],[{syncMsg.SqlId}] succeeded.");
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(new EventId(ex.HResult), ex,
                                     $"Received Invoke -> Id:{syncMsg.Id} failed, {nameof(SubscriberOptions.QueueName)}:[{QueueName}]. {Environment.NewLine} -> SyncRequest: [{message}]");
                }
            };
            _consumerChannel.BasicQos(0, 1, false);
            _consumerChannel.BasicConsume(SubscriberOptions.QueueName, true, consumer);

            _consumerChannel.CallbackException += (sender, ea) =>
            {
                _logger.LogError(ea.Exception, $"consumerChannel callback exception:{ea.Exception?.Message}");
            };
        }