Beispiel #1
0
        static void Main(string[] args)
        {
            using (IConnectionFactory connectionFactory = new SingleConnectionFactory())
            {
                IAmqpAdmin amqpAdmin = new RabbitAdmin(new RabbitTemplate(connectionFactory));

                Queue marketDataQueue = new Queue("APP.STOCK.MARKETDATA");
                amqpAdmin.DeclareQueue(marketDataQueue);
                Binding binding = new Binding(marketDataQueue, DirectExchange.DEFAULT);
                amqpAdmin.DeclareBinding(binding);

                amqpAdmin.DeclareQueue(new Queue("APP.STOCK.REQUEST"));
                amqpAdmin.DeclareQueue(new Queue("APP.STOCK.JOE"));

                //Each queue is automatically bound to the default direct exchange.

                Console.WriteLine("Queues and exchanges have been declared.");
                Console.WriteLine("Press 'enter' to exit");
                Console.ReadLine();
            }
        }
        public void TestRemoveBindingWithDefaultExchangeImplicitBinding()
        {
            var queueName = "test.queue";
            var queue = new Queue(queueName, false, false, false);
            this.rabbitAdmin.DeclareQueue(queue);
            var binding = new Binding(queueName, Binding.DestinationType.Queue, RabbitAdmin.DEFAULT_EXCHANGE_NAME, queueName, null);

            this.rabbitAdmin.RemoveBinding(binding);

            // Pass by virtue of RabbitMQ not firing a 403 reply code
        }
        public void TestSpringWithDefaultExchangeNonImplicitBinding()
        {
            var exchange = new DirectExchange(RabbitAdmin.DEFAULT_EXCHANGE_NAME);
            this.context.ObjectFactory.RegisterSingleton("foo", exchange);
            var queueName = "test.queue";
            var queue = new Queue(queueName, false, false, false);
            this.context.ObjectFactory.RegisterSingleton("bar", queue);
            var binding = new Binding(queueName, Binding.DestinationType.Queue, exchange.Name, "test.routingKey", null);
            this.context.ObjectFactory.RegisterSingleton("baz", binding);
            this.rabbitAdmin.AfterPropertiesSet();

            try
            {
                this.rabbitAdmin.DeclareBinding(binding);
            }
            catch (AmqpIOException ex)
            {
                Exception cause = ex;
                Exception rootCause = null;
                while (cause != null)
                {
                    rootCause = cause;
                    cause = cause.InnerException;
                }

                Assert.True(rootCause.Message.Contains("code=403"));
                Assert.True(rootCause.Message.Contains("operation not permitted on the default exchange"));
            }
        }
        public void TestDeclareBindingWithDefaultExchangeImplicitBinding()
        {
            var exchange = new DirectExchange(RabbitAdmin.DEFAULT_EXCHANGE_NAME);
            var queueName = "test.queue";
            var queue = new Queue(queueName, false, false, false);
            this.rabbitAdmin.DeclareQueue(queue);
            var binding = new Binding(queueName, Binding.DestinationType.Queue, exchange.Name, queueName, null);

            this.rabbitAdmin.DeclareBinding(binding);

            // Pass by virtue of RabbitMQ not firing a 403 reply code for both exchange and binding declaration
            Assert.True(this.QueueExists(queue));
        }
        public void TestSpringWithDefaultExchangeImplicitBinding()
        {
            var exchange = new DirectExchange(RabbitAdmin.DEFAULT_EXCHANGE_NAME);
            this.context.ObjectFactory.RegisterSingleton("foo", exchange);
            var queueName = "test.queue";
            var queue = new Queue(queueName, false, false, false);
            this.context.ObjectFactory.RegisterSingleton("bar", queue);
            var binding = new Binding(queueName, Binding.DestinationType.Queue, exchange.Name, queueName, null);
            this.context.ObjectFactory.RegisterSingleton("baz", binding);
            this.rabbitAdmin.AfterPropertiesSet();

            this.rabbitAdmin.Initialize();

            // Pass by virtue of RabbitMQ not firing a 403 reply code for both exchange and binding declaration
            Assert.True(this.QueueExists(queue));
        }
        private bool IsRemovingImplicitQueueBinding(Binding binding)
        {
            if (this.IsImplicitQueueBinding(binding))
            {
                Logger.Debug(m => m("Cannot remove implicit default exchange binding to queue."));
                return true;
            }

            return false;
        }
 private bool IsImplicitQueueBinding(Binding binding) { return this.IsDefaultExchange(binding.Exchange) && binding.Destination.Equals(binding.RoutingKey); }
        /// <summary>
        /// Declares all the exchanges, queues and bindings in the enclosing application context, if any. It should be safe
        /// (but unnecessary) to call this method more than once.
        /// </summary>
        public void Initialize()
        {
            if (this.applicationContext == null)
            {
                Logger.Debug(m => m("no ApplicationContext has been set, cannot auto-declare Exchanges, Queues, and Bindings"));
                return;
            }

            Logger.Debug(m => m("Initializing declarations"));
            var exchanges = this.applicationContext.GetObjectsOfType(typeof(IExchange)).Values;
            var queues = this.applicationContext.GetObjectsOfType(typeof(Queue)).Values;
            var bindings = this.applicationContext.GetObjectsOfType(typeof(Binding)).Values;

            foreach (IExchange exchange in exchanges)
            {
                if (!exchange.Durable)
                {
                    Logger.Warn(m => m("Auto-declaring a non-durable Exchange ({0}). It will be deleted by the broker if it shuts down, and can be redeclared by closing and reopening the connection.", exchange.Name));
                }

                if (exchange.AutoDelete)
                {
                    Logger.Warn(
                        m =>
                        m("Auto-declaring an auto-delete Exchange ({0}). It will be deleted by the broker if not in use (if all bindings are deleted), but will only be redeclared if the connection is closed and reopened.", exchange.Name));
                }
            }

            foreach (Queue queue in queues)
            {
                if (!queue.Durable)
                {
                    Logger.Warn(m => m("Auto-declaring a non-durable Queue ({0}). It will be redeclared if the broker stops and is restarted while the connection factory is alive, but all messages will be lost.", queue.Name));
                }

                if (queue.AutoDelete)
                {
                    Logger.Warn(m => m("Auto-declaring an auto-delete Queue ({0}). It will be deleted by the broker if not in use, and all messages will be lost.  Redeclared when the connection is closed and reopened.", queue.Name));
                }

                if (queue.Exclusive)
                {
                    Logger.Warn(m => m("Auto-declaring an exclusive Queue ({0}). It cannot be accessed by consumers on another connection, and will be redeclared if the connection is reopened.", queue.Name));
                }
            }

            this.rabbitTemplate.Execute<object>(
                channel =>
                {
                    var exchangeArray = new IExchange[exchanges.Count];
                    var queueArray = new Queue[queues.Count];
                    var bindingArray = new Binding[bindings.Count];

                    exchanges.CopyTo(exchangeArray, 0);
                    queues.CopyTo(queueArray, 0);
                    bindings.CopyTo(bindingArray, 0);

                    this.DeclareExchanges(channel, exchangeArray);
                    this.DeclareQueues(channel, queueArray);
                    this.DeclareBindings(channel, bindingArray);
                    return null;
                });

            Logger.Debug(m => m("Declarations finished"));
        }
        private bool IsDeclaringImplicitQueueBinding(Binding binding)
        {
            if (this.IsImplicitQueueBinding(binding))
            {
                Logger.Debug(m => m("The default exchange is implicitly bound to every queue, with a routing key equal to the queue name."));
                return true;
            }

            return false;
        }
        /// <summary>Remove a binding of a queue to an exchange.</summary>
        /// <param name="binding">Binding to remove.</param>
        public void RemoveBinding(Binding binding)
        {
            this.rabbitTemplate.Execute<object>(
                channel =>
                {
                    if (binding.IsDestinationQueue())
                    {
                        if (this.IsRemovingImplicitQueueBinding(binding))
                        {
                            return null;
                        }

                        channel.QueueUnbind(binding.Destination, binding.Exchange, binding.RoutingKey, binding.Arguments);
                    }
                    else
                    {
                        channel.ExchangeUnbind(binding.Destination, binding.Exchange, binding.RoutingKey, binding.Arguments);
                    }

                    return null;
                });
        }
 /// <summary>Declare the binding.</summary>
 /// <param name="binding">The binding.</param>
 public void DeclareBinding(Binding binding)
 {
     this.rabbitTemplate.Execute<object>(
         channel =>
         {
             this.DeclareBindings(channel, binding);
             return null;
         });
 }
 public void RemoveBinding(Binding binding)
 {
     rabbitTemplate.Execute<object>(delegate(IModel model)
                                        {
                                            model.QueueUnbind(binding.Queue, binding.Exchange, binding.RoutingKey,
                                                              binding.Arguments);
                                            return null;
                                        });
 }
 public void DeclareBinding(Binding binding)
 {
     rabbitAdmin.DeclareBinding(binding);
 }
Beispiel #14
0
 public void DeclareBinding(Binding binding)
 {
     rabbitTemplate.Execute<object>(delegate(IModel channel)
     {
         channel.QueueBind(binding.Queue, binding.Exchange, binding.RoutingKey, false, binding.Arguments);
         return null;
     });
 }