Beispiel #1
0
        protected override void DeclareQueue <T>(QueueSetupData queue, string queueName, IModel model)
        {
            SetQueueSetupArguments(queue);

            if (queue is PriorityQueueSetupData)
            {
                var maxPriority = (queue as PriorityQueueSetupData).MaxPriorityLevel;
                for (uint i = 0; i <= maxPriority; i++)
                {
                    try
                    {
                        model.QueueDeclare(GetPriorityQueueName <T>((PriorityQueueSetupData)queue, queueName, i), queue.Durable, false, queue.AutoDelete, queue.Arguments);
                    }
                    catch (OperationInterruptedException oie)
                    {
                        if (oie.ShutdownReason.ReplyText.StartsWith("PRECONDITION_FAILED - "))
                        {
                            _watcher.WarnFormat(oie.ShutdownReason.ReplyText);
                        }
                        else
                        {
                            _watcher.Error(oie);
                        }
                    }
                    catch (Exception ex)
                    {
                        _watcher.Error(ex);
                    }
                }
            }
            else
            {
                base.DeclareQueue <T>(queue, queueName, model);
            }
        }
Beispiel #2
0
 protected virtual void DeclareQueue <T>(QueueSetupData queue, string queueName, IModel model)
 {
     try
     {
         IDictionary arguments = new Dictionary <string, object>();
         if (queue.MessageTimeToLive > 0)
         {
             arguments.Add("x-message-ttl", queue.MessageTimeToLive);
         }
         if (queue.AutoExpire > 0)
         {
             arguments.Add("x-expires", queue.AutoExpire);
         }
         model.QueueDeclare(queueName, queue.Durable, false, queue.AutoDelete, arguments);
     }
     catch (OperationInterruptedException oie)
     {
         if (oie.ShutdownReason.ReplyText.StartsWith("PRECONDITION_FAILED - "))
         {
             _watcher.ErrorFormat(oie.ShutdownReason.ReplyText);
         }
         else
         {
             _watcher.Error(oie);
         }
     }
     catch (Exception ex)
     {
         _watcher.Error(ex);
     }
 }
Beispiel #3
0
        public void SetupExchangeAndQueueFor <T>(ExchangeSetupData exchange, QueueSetupData queue)
        {
            var routeFinder  = _routeFinderFactory(_environment, exchange.ExchangeType);
            var queueName    = routeFinder.FindQueueName <T>(queue.SubscriptionName);
            var exchangeName = routeFinder.FindExchangeName <T>();
            var routingKey   = queue.RoutingKey ?? routeFinder.FindRoutingKey <T>();

            using (var connection = _connectionFactory.CreateConnection())
            {
                using (var model = connection.CreateModel())
                {
                    // Declare Exchange
                    DeclareExchange(exchange, model, exchangeName);
                }
                using (var model = connection.CreateModel())
                {
                    // Declare Queue
                    DeclareQueue <T>(queue, queueName, model);
                }
                using (var model = connection.CreateModel())
                {
                    // Bind Queue to Exchange
                    BindQueue <T>(model, queue, exchangeName, queueName, routingKey);
                }
            }
        }
Beispiel #4
0
 protected override void DeleteQueue <T>(IModel model, QueueSetupData queue, string queueName)
 {
     if (queue is PriorityQueueSetupData)
     {
         var maxPriority = (queue as PriorityQueueSetupData).MaxPriorityLevel;
         for (uint i = 0; i <= maxPriority; i++)
         {
             try
             {
                 model.QueueDelete(GetPriorityQueueName <T>((PriorityQueueSetupData)queue, queueName, i));
             }
             catch (OperationInterruptedException oie)
             {
                 if (oie.ShutdownReason.ReplyText.StartsWith("NOT_FOUND - no queue "))
                 {
                     _watcher.WarnFormat(oie.ShutdownReason.ReplyText);
                 }
                 else
                 {
                     _watcher.Error(oie);
                 }
             }
             catch (Exception ex)
             {
                 _watcher.Error(ex);
             }
         }
     }
     else
     {
         base.DeleteQueue <T>(model, queue, queueName);
     }
 }
Beispiel #5
0
 protected override void BindQueue <T>(IModel model, QueueSetupData queue, string exchangeName, string queueName, string routingKey, IDictionary <string, object> bindingData = null)
 {
     if (queue is PriorityQueueSetupData)
     {
         var maxPriority = (queue as PriorityQueueSetupData).MaxPriorityLevel;
         for (uint i = 0; i <= maxPriority; i++)
         {
             try
             {
                 var arguments = GetArgumentDictionary(bindingData);
                 arguments["x-match"]    = "all";
                 arguments["Priority"]   = i.ToString(CultureInfo.InvariantCulture);
                 arguments["RoutingKey"] = routingKey;
                 //http://www.rabbitmq.com/tutorials/amqp-concepts.html
                 //http://lostechies.com/derekgreer/2012/03/28/rabbitmq-for-windows-exchange-types/
                 model.QueueBind(GetPriorityQueueName <T>((PriorityQueueSetupData)queue, queueName, i), exchangeName, routingKey /*It'll be ignored as AMQP spec*/, arguments);
             }
             catch (Exception ex)
             {
                 _watcher.Error(ex);
             }
         }
     }
     else
     {
         base.BindQueue <T>(model, queue, exchangeName, queueName, routingKey, bindingData);
     }
 }
 protected override void BindQueue <T>(RabbitMQ.Client.IModel model, QueueSetupData queue, string exchangeName, string queueName, string routingKey)
 {
     if (queue is PriorityQueueSetupData)
     {
         var maxPriority = (queue as PriorityQueueSetupData).MaxPriorityLevel;
         for (uint i = 0; i <= maxPriority; i++)
         {
             try
             {
                 IDictionary arguments = new HybridDictionary();
                 arguments.Add("x-match", "all");
                 arguments.Add("Priority", i.ToString(CultureInfo.InvariantCulture));
                 arguments.Add("RoutingKey", routingKey);
                 //http://www.rabbitmq.com/tutorials/amqp-concepts.html
                 //http://lostechies.com/derekgreer/2012/03/28/rabbitmq-for-windows-exchange-types/
                 model.QueueBind(GetPriorityQueueName <T>(queueName, i), exchangeName, routingKey /*It'll be ignored as AMQP spec*/, arguments);
             }
             catch (Exception ex)
             {
                 _watcher.Error(ex);
             }
         }
     }
     else
     {
         base.BindQueue <T>(model, queue, exchangeName, queueName, routingKey);
     }
 }
Beispiel #7
0
        public void Should_bind_with_provided_params()
        {
            // Arrange
            var model = Substitute.For<IModel>();
            var setup = RabbitSetupForTest.CreateRabbitSetup(model);

            // Action
            var queueSetupData = new QueueSetupData();
            queueSetupData.Arguments.Add("Key1", "Val1");
            setup.BindQueue<Customer>(model, queueSetupData, "ExchangeName", "QueueName", "RoutingKey");

            // Assert
            model.Received().QueueBind(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Is<IDictionary<string, object>>(arg => arg["Key1"] == "Val1"));
        }
Beispiel #8
0
        protected virtual void BindQueue <T>(IModel model, QueueSetupData queue, string exchangeName, string queueName, string routingKey)
        {
            if (string.IsNullOrEmpty(exchangeName))
            {
                _watcher.WarnFormat("Attempt to bind queue {0} to a empty name Exchange, that's the default built-in exchange so the action will be ignored", queueName);
                return;
            }

            try
            {
                model.QueueBind(queueName, exchangeName, routingKey);
            }
            catch (Exception ex)
            {
                _watcher.Error(ex);
            }
        }
        protected override void DeclareQueue <T>(QueueSetupData queue, string queueName, RabbitMQ.Client.IModel model)
        {
            IDictionary arguments = new Dictionary <string, object>();

            if (queue.MessageTimeToLive > 0)
            {
                arguments.Add("x-message-ttl", queue.MessageTimeToLive);
            }
            if (queue.AutoExpire > 0)
            {
                arguments.Add("x-expires", queue.AutoExpire);
            }

            if (queue is PriorityQueueSetupData)
            {
                var maxPriority = (queue as PriorityQueueSetupData).MaxPriorityLevel;
                for (uint i = 0; i <= maxPriority; i++)
                {
                    try
                    {
                        model.QueueDeclare(GetPriorityQueueName <T>(queueName, i), queue.Durable, false,
                                           queue.AutoDelete, arguments);
                    }
                    catch (OperationInterruptedException oie)
                    {
                        if (oie.ShutdownReason.ReplyText.StartsWith("PRECONDITION_FAILED - "))
                        {
                            _watcher.WarnFormat(oie.ShutdownReason.ReplyText);
                        }
                        else
                        {
                            _watcher.Error(oie);
                        }
                    }
                    catch (Exception ex)
                    {
                        _watcher.Error(ex);
                    }
                }
            }
            else
            {
                base.DeclareQueue <T>(queue, queueName, model);
            }
        }
Beispiel #10
0
 protected void SetQueueSetupArguments(QueueSetupData queue)
 {
     if (queue.MessageTimeToLive > 0)
     {
         queue.Arguments["x-message-ttl"] = queue.MessageTimeToLive;
     }
     if (queue.AutoExpire > 0)
     {
         queue.Arguments["x-expires"] = queue.AutoExpire;
     }
     if (queue.DeadLetterExchange != null)
     {
         queue.Arguments["x-dead-letter-exchange"] = queue.DeadLetterExchange;
     }
     if (queue.DeadLetterRoutingKey != null)
     {
         queue.Arguments["x-dead-letter-routing-key"] = queue.DeadLetterRoutingKey;
     }
 }
Beispiel #11
0
 protected virtual void DeleteQueue <T>(IModel model, QueueSetupData queue, string queueName)
 {
     try
     {
         model.QueueDelete(queueName);
     }
     catch (OperationInterruptedException oie)
     {
         if (oie.ShutdownReason.ReplyText.StartsWith("NOT_FOUND - no queue "))
         {
             _watcher.WarnFormat(oie.ShutdownReason.ReplyText);
         }
         else
         {
             _watcher.Error(oie);
         }
     }
     catch (Exception ex)
     {
         _watcher.Error(ex);
     }
 }
Beispiel #12
0
        public virtual void Destroy <T>(ExchangeSetupData exchange, QueueSetupData queue)
        {
            var conventions  = _routeFinderFactory(_environment, exchange.ExchangeType);
            var queueName    = conventions.FindQueueName <T>(queue.SubscriptionName);
            var exchangeName = conventions.FindExchangeName <T>();

            using (var connection = _connectionFactory.CreateConnection())
            {
                using (var model = connection.CreateModel())
                {
                    // Delete Queue
                    DeleteQueue <T>(model, queue, queueName);
                }

                using (var model = connection.CreateModel())
                {
                    // Delete Exchange
                    try
                    {
                        model.ExchangeDelete(exchangeName);
                    }
                    catch (OperationInterruptedException oie)
                    {
                        if (oie.ShutdownReason.ReplyText.StartsWith("NOT_FOUND - no exchange "))
                        {
                            _watcher.WarnFormat(oie.ShutdownReason.ReplyText);
                        }
                        else
                        {
                            _watcher.Error(oie);
                        }
                    }
                    catch (Exception ex)
                    {
                        _watcher.Error(ex);
                    }
                }
            }
        }
Beispiel #13
0
 protected virtual void DeclareQueue <T>(QueueSetupData queue, string queueName, IModel model)
 {
     try
     {
         SetQueueSetupArguments(queue);
         model.QueueDeclare(queueName, queue.Durable, false, queue.AutoDelete, queue.Arguments);
     }
     catch (OperationInterruptedException oie)
     {
         if (oie.ShutdownReason.ReplyText.StartsWith("PRECONDITION_FAILED - "))
         {
             _watcher.ErrorFormat(oie.ShutdownReason.ReplyText);
         }
         else
         {
             _watcher.Error(oie);
         }
     }
     catch (Exception ex)
     {
         _watcher.Error(ex);
     }
 }