예제 #1
0
        private async Task ProcessMessageAsync(object model, BasicDeliverEventArgs rabbitMessage, Func <QueueMessage <T>, CancellationToken, Task> onMessageReceived, CancellationToken cancellationToken)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (rabbitMessage == null)
            {
                throw new ArgumentNullException(nameof(rabbitMessage));
            }

            if (onMessageReceived == null)
            {
                throw new ArgumentNullException(nameof(onMessageReceived));
            }

            try
            {
                // Deserialize object
                T messageObject = _serializer.Deserialize <T>(Encoding.UTF8.GetString(rabbitMessage.Body));

                // Validate object
                string validationErrors;
                bool   success = _validationHelper.TryValidate(messageObject, out validationErrors);

                if (!success)
                {
                    _logger.ErrorFormat(
                        Resources.MessageFailsValidationLogEntry,
                        rabbitMessage.DeliveryTag,
                        _queueName,
                        validationErrors);
                    NegativelyAcknowledge(rabbitMessage.DeliveryTag);
                    return;
                }

                _logger.InfoFormat(Resources.MessageSuccessfullyReceivedLogEntry, rabbitMessage.DeliveryTag, _queueName);
                var message = new QueueMessage <T>(messageObject,
                                                   rabbitMessage.DeliveryTag,
                                                   rabbitMessage.RoutingKey,
                                                   rabbitMessage.BasicProperties.Headers);

                // call the event handler to process the message
                await onMessageReceived(message, cancellationToken);
            }
            catch (Exception ex)
            {
                _logger.ErrorFormat(
                    Resources.MessageFailsValidationLogEntry,
                    rabbitMessage.DeliveryTag,
                    _queueName,
                    ex.Message);

                NegativelyAcknowledge(rabbitMessage.DeliveryTag);
            }
        }
예제 #2
0
        public void Publish(T message, IDictionary <string, object> headers, string dynamicRoutingKey)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            try
            {
                // Validate message
                string validationErrors;
                if (!_validationHelper.TryValidate(message, out validationErrors))
                {
                    throw new ValidationException(validationErrors);
                }

                // serialise object...
                string messageBody = _serializer.SerializeObject(message);

                // Determine routing key
                var routingKey = dynamicRoutingKey ?? _publisherConfig.RoutingKey;

                _logger.DebugFormat(Resources.PublishingMessageLogEntry, _publisherConfig.ExchangeName, routingKey, messageBody);

                lock (_lock)
                {
                    if (!_connected)
                    {
                        _connection = _connectionFactory.CreateConnection(_publisherConfig.Name, _cancellationToken);

                        _channel = _connection.CreateModel();

                        _connected = true;
                    }
                }

                // Create message properties
                var basicProperties = CreateBasicProperties(headers);

                _channel.BasicPublish(_publisherConfig.ExchangeName,
                                      routingKey,
                                      true,
                                      basicProperties,
                                      Encoding.UTF8.GetBytes(messageBody));

                _logger.Info($"Sent message");
            }
            catch (Exception ex)
            {
                _logger.Error($"An unexpected exception occurred, error details '{ex.Message}'", ex);

                lock (_lock)
                {
                    _connected = false;
                }

                if (_channel != null)
                {
                    _channel.Dispose();
                }

                if (_connection != null)
                {
                    _connection.Dispose();
                }
            }
        }