Example #1
0
        //NOTE This is the only method that cannot be moved into RestBus.Common so keep that in mind if intergrating other Amqp brokers
        public static void DeclareExchangeAndQueues(IModel channel, ExchangeInfo exchangeInfo, object syncObject, string subscriberId)
        {
            //TODO: IS the lock statement here necessary?
            lock (syncObject)
            {
                string exchangeName  = AmqpUtils.GetExchangeName(exchangeInfo);
                string workQueueName = AmqpUtils.GetWorkQueueName(exchangeInfo);

                if (exchangeInfo.Exchange != "")
                {
                    //TODO: If Queues are durable then exchange ought to be too.
                    channel.ExchangeDeclare(exchangeName, exchangeInfo.ExchangeType, false, true, null);
                }

                var workQueueArgs = new Dictionary <string, object>();
                workQueueArgs.Add("x-expires", (long)AmqpUtils.GetWorkQueueExpiry().TotalMilliseconds);

                //TODO: the line below can throw some kind of socket exception, so what do you do in that situation
                //Bear in mind that Restart may call this code.
                //The exception name is the OperationInterruptedException

                //Declare work queue
                channel.QueueDeclare(workQueueName, false, false, false, workQueueArgs);
                channel.QueueBind(workQueueName, exchangeName, AmqpUtils.GetWorkQueueRoutingKey());

                if (subscriberId != null)
                {
                    string subscriberQueueName = AmqpUtils.GetSubscriberQueueName(exchangeInfo, subscriberId);

                    var subscriberQueueArgs = new Dictionary <string, object>();
                    subscriberQueueArgs.Add("x-expires", (long)AmqpUtils.GetSubscriberQueueExpiry().TotalMilliseconds);

                    //TODO: Look into making the subscriber queue exclusive
                    //and retry with a different id if the queue has alreasy been taken.
                    //in this case the Id property of the ISubscriber interface should be changed to GetId()
                    //and will be documented to return the "current" id.
                    //In fact Hide GetId and id property for both client and subscriber, they should be private for now.
                    //Write something similar for the client.

                    //TODO: the line below can throw some kind of socket exception, so what do you do in that situation
                    //Bear in mind that Restart may call this code.
                    //The exception name is the OperationInterruptedException

                    //Declare subscriber queue
                    channel.QueueDeclare(subscriberQueueName, false, false, true, subscriberQueueArgs);
                    channel.QueueBind(subscriberQueueName, exchangeName, subscriberId);
                }
            }
        }
Example #2
0
        //NOTE This is the only method that cannot be moved into RestBus.Common so keep that in mind if integrating other Amqp brokers
        public static void DeclareExchangeAndQueues(IModel channel, IMessageMapper messageMapper, MessagingConfiguration messagingConfig, string serviceName, object syncObject, string subscriberId)
        {
            //TODO: IS the lock statement here necessary?
            lock (syncObject)
            {
                //TODO: Other Exchange types.

                string exchangeName  = AmqpUtils.GetExchangeName(messagingConfig, serviceName, ExchangeKind.Direct);
                string workQueueName = AmqpUtils.GetWorkQueueName(messagingConfig, serviceName);

                if (serviceName != "")
                {
                    //Declare direct exchange
                    if (messageMapper.SupportedExchangeKinds.HasFlag(ExchangeKind.Direct))
                    {
                        channel.ExchangeDeclare(exchangeName, AmqpUtils.GetExchangeKindName(ExchangeKind.Direct), messagingConfig.PersistentWorkQueuesAndExchanges, !messagingConfig.PersistentWorkQueuesAndExchanges, null);
                    }
                }

                Dictionary <string, object> workQueueArgs = null;
                if (!messagingConfig.PersistentWorkQueuesAndExchanges)
                {
                    workQueueArgs = new Dictionary <string, object>();
                    workQueueArgs.Add("x-expires", (long)AmqpUtils.GetWorkQueueExpiry().TotalMilliseconds);
                }

                //TODO: the line below can throw some kind of socket exception, so what do you do in that situation
                //Bear in mind that Restart may call this code.
                //The exception name is the OperationInterruptedException

                //Declare work queue
                channel.QueueDeclare(workQueueName, messagingConfig.PersistentWorkQueuesAndExchanges, false, false, workQueueArgs);
                channel.QueueBind(workQueueName, exchangeName, AmqpUtils.GetWorkQueueRoutingKey());

                if (subscriberId != null)
                {
                    string subscriberQueueName = AmqpUtils.GetSubscriberQueueName(serviceName, subscriberId);

                    //The queue is set to be auto deleted once the last consumer stops using it.
                    //However, RabbitMQ will not delete the queue if no consumer ever got to use it.
                    //Passing x-expires in solves that: It tells RabbitMQ to delete the queue, if no one uses it within the specified time.

                    var subscriberQueueArgs = new Dictionary <string, object>();
                    subscriberQueueArgs.Add("x-expires", (long)AmqpUtils.GetSubscriberQueueExpiry().TotalMilliseconds);

                    //TODO: Look into making the subscriber queue exclusive
                    //and retry with a different id if the queue has alreasy been taken.
                    //in this case the Id property of the ISubscriber interface should be changed to GetId()
                    //and will be documented to return the "current" id.
                    //In fact Hide GetId and id property for both client and subscriber, they should be private for now.
                    //Write something similar for the client.

                    //TODO: the line below can throw some kind of socket exception, so what do you do in that situation
                    //Bear in mind that Restart may call this code.
                    //The exception name is the OperationInterruptedException

                    //Declare subscriber queue
                    channel.QueueDeclare(subscriberQueueName, false, false, true, subscriberQueueArgs);
                    channel.QueueBind(subscriberQueueName, exchangeName, subscriberId);
                }
            }
        }