Exemple #1
0
        public void Publish(IntegrationMessage message)
        {
            if (!_persistentConnection.IsConnected)
            {
                _persistentConnection.TryConnect();
            }

            var policy = Policy.Handle <BrokerUnreachableException>()
                         .Or <SocketException>()
                         .WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                                       (ex, time) =>
            {
                _logger.LogWarning(ex,
                                   "Could not publish event: {EventId} after {Timeout}s ({ExceptionMessage})", message.Id,
                                   $"{time.TotalSeconds:n1}", ex.Message);
            });

            var eventName = message.GetType().Name;

            _logger.LogTrace("Creating RabbitMQ channel to publish event: {EventId} ({EventName})", message.Id,
                             eventName);

            using (var channel = _persistentConnection.CreateModel())
            {
                _logger.LogTrace("Declaring RabbitMQ exchange to publish event: {EventId}", message.Id);

                channel.ExchangeDeclare(BROKER_NAME, "direct");

                var messageSerialized = JsonConvert.SerializeObject(message);
                var body = Encoding.UTF8.GetBytes(messageSerialized);

                policy.Execute(() =>
                {
                    var properties          = channel.CreateBasicProperties();
                    properties.DeliveryMode = 2; // persistent

                    _logger.LogTrace("Publishing event to RabbitMQ: {EventId}", message.Id);

                    channel.BasicPublish(
                        BROKER_NAME,
                        eventName,
                        true,
                        properties,
                        body);
                });
            }
        }
 public static string GetMessageName(this IntegrationMessage?message)
 => message?.GetType().Name ?? FallBackMessageName;
Exemple #3
0
 public string PackAsJson(IntegrationMessage message)
 => JsonSerializer.Serialize(message, message.GetType());