internal static void DeclareExchangeForConfiguration(this IModel channel, RMQMessagingGatewayConfigurationSection configuration)
 {
     if (configuration.Exchange.SupportDelay)
         channel.ExchangeDeclare(configuration.Exchange.Name, "x-delayed-message", configuration.Exchange.Durable, autoDelete: false, arguments: new Dictionary<string, object> { { "x-delayed-type", "direct" } });
     else
         channel.ExchangeDeclare(configuration.Exchange.Name, ExchangeType.Direct, configuration.Exchange.Durable);
 }
Example #2
0
 private TestRMQListener(string channelName, RMQMessagingGatewayConfigurationSection configuration)
 {
     _channelName = channelName;
     rMQMessagingGatewayConfigurationSection = configuration;
     _connectionFactory = new ConnectionFactory {Uri = configuration.AMPQUri.Uri.ToString()};
     _connection = _connectionFactory.CreateConnection();
     _channel = _connection.CreateModel();
     _channel.DeclareExchangeForConfiguration(configuration);
     _channel.QueueDeclare(_channelName, false, false, false, null);
     _channel.QueueBind(_channelName, configuration.Exchange.Name, _channelName);
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="ConnectionPolicyFactory"/> class. 
        /// Use if you need to inject a test logger
        /// </summary>
        /// <param name="logger">The logger.</param>
        /// <param name="configurationSection"></param>
        public ConnectionPolicyFactory(ILog logger, RMQMessagingGatewayConfigurationSection configurationSection)
        {
            _logger = logger;

            var configuration = configurationSection;
            int retries = configuration.AMPQUri.ConnectionRetryCount;
            int retryWaitInMilliseconds = configuration.AMPQUri.RetryWaitInMilliseconds;
            int circuitBreakerTimeout = configuration.AMPQUri.CircuitBreakTimeInMilliseconds;

            RetryPolicy = Policy.Handle<BrokerUnreachableException>()
                .Or<Exception>()
                .WaitAndRetry(
                    retries,
                    retryAttempt => TimeSpan.FromMilliseconds(retryWaitInMilliseconds * Math.Pow(2, retryAttempt)),
                    (exception, timeSpan, context) =>
                    {
                        if (exception is BrokerUnreachableException)
                        {
                            _logger.WarnException(
                                "RMQMessagingGateway: BrokerUnreachableException error on connecting to queue {0} exchange {1} on connection {2}. Will retry {3} times",
                                exception,
                                context["queueName"],
                                configuration.Exchange.Name,
                                configuration.AMPQUri.GetSanitizedUri(),
                                retries);
                        }
                        else
                        {
                            logger.WarnException(
                                "RMQMessagingGateway: Exception on connection to queue {0} via exchange {1} on connection {2}",
                                exception,
                                context["queueName"],
                                configuration.Exchange.Name,
                                configuration.AMPQUri.GetSanitizedUri());
                            throw exception;
                        }
                    });

            CircuitBreakerPolicy = Policy.Handle<BrokerUnreachableException>().CircuitBreaker(1, TimeSpan.FromMilliseconds(circuitBreakerTimeout));
        }
 public RMQMessagingGateway(ILog logger)
 {
     this.logger = logger;
     configuration = RMQMessagingGatewayConfigurationSection.GetConfiguration();
     connectionFactory = new ConnectionFactory{Uri = configuration.AMPQUri.Uri.ToString()};
 }
 private void PublishMessage(Message message, IModel channel, RMQMessagingGatewayConfigurationSection configuration, IBasicProperties basicProperties)
 {
     //publish message
     channel.BasicPublish(configuration.Exchange.Name, message.Header.Topic, false, false, basicProperties, Encoding.UTF8.GetBytes(message.Body.Value));
 }
 private void DeclareExchange(IModel channel, RMQMessagingGatewayConfigurationSection configuration)
 {
     //desired state configuration of the exchange
     channel.ExchangeDeclare(configuration.Exchange.Name, ExchangeType.Direct, false);
 }
Example #7
0
        private MessageGateway(ILog logger, RMQMessagingGatewayConfigurationSection configuration)
        {
            Logger = logger;
            Configuration = configuration;

            var connectionPolicyFactory = new ConnectionPolicyFactory(logger, Configuration);

            _retryPolicy = connectionPolicyFactory.RetryPolicy;
            _circuitBreakerPolicy = connectionPolicyFactory.CircuitBreakerPolicy;

            _connectionFactory = new ConnectionFactory { Uri = Configuration.AMPQUri.Uri.ToString(), RequestedHeartbeat = 30 };

            DelaySupported = (this is IAmAMessageGatewaySupportingDelay) && Configuration.Exchange.SupportDelay;

        }