Beispiel #1
0
        public Task PublishAsync(SyncRequest syncRequest)
        {
            if (!_connection.IsConnected)
            {
                _connection.TryConnect();
            }

            var channel = GetChannel();

            channel.ExchangeDeclare(_rabbitMqOptions.Exchange,
                                    _rabbitMqOptions.ExchangeType, true, false, null);

            var data = JsonConvert.SerializeObject(syncRequest);
            var body = Encoding.UTF8.GetBytes(data);

            channel.BasicPublish(_rabbitMqOptions.Exchange,
                                 _rabbitMqOptions.RoutingKey,
                                 false,
                                 new BasicProperties {
                Persistent = true
            },
                                 body);

            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug(
                    $"Publish SyncRequest -> Id:{syncRequest.Id} succeeded, {nameof(RabbitMQOptions.Exchange)}:[{_rabbitMqOptions.Exchange}], {nameof(RabbitMQOptions.ExchangeType)}:[{_rabbitMqOptions.ExchangeType}], {nameof(RabbitMQOptions.RoutingKey)}:[{_rabbitMqOptions.RoutingKey}].");
            }

            return(Task.CompletedTask);
        }
Beispiel #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}");
            };
        }