Exemple #1
0
        public void Publish(IntegrationEvent @event)
        {
            var rabbitMQEventAttribute = GetRabbitMQEventAttribute(@event.GetType());
            var persistentConnection   = GetRabbitMQPersistentConnection(rabbitMQEventAttribute);

            if (!persistentConnection.IsConnected)
            {
                persistentConnection.TryConnect();
            }

            var policy = RetryPolicy.Handle <BrokerUnreachableException>()
                         .Or <SocketException>()
                         .WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) =>
            {
                _logger.LogWarning(ex.ToString());
            });

            using (var channel = persistentConnection.CreateModel())
            {
                var eventName = @event.GetType().Name;

                //channel.ExchangeDeclare(exchange: rabbitMQEventAttribute.Exchange,
                //                    type: "fanout");

                var body = @event.ToProtoBufBytes();

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

                    channel.BasicPublish(exchange: rabbitMQEventAttribute.Exchange,
                                         routingKey: rabbitMQEventAttribute.RoutingKey,
                                         mandatory: true,
                                         basicProperties: properties,
                                         body: body);
                });
            }
        }