/// <summary>
 /// Initializes a new instance of the <see cref="MessageGateway"/> class.
 /// </summary>
 /// <param name="logger">The logger.</param>
 public MessageGateway(ILog logger)
 {
     this.Logger       = logger;
     Configuration     = RMQMessagingGatewayConfigurationSection.GetConfiguration();
     connectionFactory = new ConnectionFactory {
         Uri = Configuration.AMPQUri.Uri.ToString()
     };
 }
Exemple #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);
 }
 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", configuration.Exchange.Type }
         });
     }
     else
     {
         channel.ExchangeDeclare(configuration.Exchange.Name, configuration.Exchange.Type, configuration.Exchange.Durable);
     }
 }
Exemple #4
0
        public MessageGateway(ILog logger)
        {
            this.Logger   = logger;
            Configuration = RMQMessagingGatewayConfigurationSection.GetConfiguration();

            var connectionPolicyFactory = new ConnectionPolicyFactory(logger);

            retryPolicy          = connectionPolicyFactory.RetryPolicy;
            circuitBreakerPolicy = connectionPolicyFactory.CircuitBreakerPolicy;

            connectionFactory = new ConnectionFactory {
                Uri = Configuration.AMPQUri.Uri.ToString(), RequestedHeartbeat = 30
            };
        }
        public TestRMQListener(string channelName)
        {
            this.channelName = channelName;
            var configuration = RMQMessagingGatewayConfigurationSection.GetConfiguration();

            connectionFactory = new ConnectionFactory {
                Uri = configuration.AMPQUri.Uri.ToString()
            };
            connection = connectionFactory.CreateConnection();
            channel    = connection.CreateModel();
            channel.ExchangeDeclare(configuration.Exchange.Name, ExchangeType.Direct, false);
            channel.QueueDeclare(this.channelName, false, false, false, null);
            channel.QueueBind(this.channelName, configuration.Exchange.Name, this.channelName);
        }
Exemple #6
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;
        }
Exemple #7
0
        /// <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));
        }
Exemple #8
0
 void DeclareExchange(IModel channel, RMQMessagingGatewayConfigurationSection configuration)
 {
     //desired state configuration of the exchange
     channel.ExchangeDeclare(configuration.Exchange.Name, ExchangeType.Direct, configuration.Exchange.Durable);
 }
 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));
 }
Exemple #10
0
 protected MessageGateway(ILog logger, string connectionName) : this(logger, RMQMessagingGatewayConfigurationSection.GetConfiguration(connectionName))
 {
 }
Exemple #11
0
 protected MessageGateway(string connectionName)
     : this(LogProvider.For <MessageGateway>(), RMQMessagingGatewayConfigurationSection.GetConfiguration(connectionName))
 {
 }
Exemple #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MessageGateway"/> class.
 /// Use if you need to inject a test logger
 /// </summary>
 /// <param name="logger">The logger.</param>
 public MessageGateway(ILog logger) : this(logger, RMQMessagingGatewayConfigurationSection.GetConfiguration())
 {
 }
Exemple #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConnectionPolicyFactory"/> class.
 /// </summary>
 /// <param name="logger">The logger.</param>
 public ConnectionPolicyFactory()
     : this(LogProvider.For <ConnectionPolicyFactory>(), RMQMessagingGatewayConfigurationSection.GetConfiguration())
 {
 }
Exemple #14
0
 public TestRMQListener(string channelName, string connectionName) : this(channelName, RMQMessagingGatewayConfigurationSection.GetConfiguration(connectionName))
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConnectionPolicyFactory"/> class.
 /// </summary>
 /// <param name="logger">The logger.</param>
 public ConnectionPolicyFactory()
     : this(LogProvider.GetCurrentClassLogger(), RMQMessagingGatewayConfigurationSection.GetConfiguration())
 {
 }
Exemple #16
0
 protected MessageGateway(string connectionName)
     : this(LogProvider.GetCurrentClassLogger(), RMQMessagingGatewayConfigurationSection.GetConfiguration(connectionName))
 {
 }