Beispiel #1
0
        /// <summary>
        /// Create Exchange and queue using RouteSetupData
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="routeSetupData"></param>
        public void CreateRoute <T>(RouteSetupData routeSetupData)
        {
            var queueName    = routeSetupData.RouteFinder.FindQueueName <T>(routeSetupData.SubscriptionName);
            var exchangeName = routeSetupData.RouteFinder.FindExchangeName <T>();
            var routingKey   = routeSetupData.RouteFinder.FindRoutingKey <T>();

            using (var connection = ConnectionFactory.CreateConnection())
            {
                using (var model = connection.CreateModel())
                {
                    // Declare Exchange
                    DeclareExchange(routeSetupData.ExchangeSetupData, model, exchangeName);
                }
                using (var model = connection.CreateModel())
                {
                    // Declare Queue
                    DeclareQueue <T>(routeSetupData.QueueSetupData, queueName, model);
                }
                using (var model = connection.CreateModel())
                {
                    // Bind Queue to Exchange
                    BindQueue <T>(model, routeSetupData.QueueSetupData, exchangeName, queueName, routingKey, routeSetupData.OptionalBindingData);
                }
            }
        }
Beispiel #2
0
        public void Setup()
        {
            _routeFinder.FindExchangeName<Customer>().Returns("Exchange.Customer");
            _routeFinder.FindQueueName<Customer>(null).ReturnsForAnyArgs("Queue.Customer");
            _routeFinder.FindRoutingKey<Customer>().Returns("Customer");

            _routeSetupData = new RouteSetupData
                                 {
                                     RouteFinder = _routeFinder,
                                     ExchangeSetupData = new ExchangeSetupData(),
                                     QueueSetupData = new QueueSetupData()
                                 };
        }
        public void Setup()
        {
            _routeFinder.FindExchangeName<Customer>().Returns("Exchange.Customer");
            _routeFinder.FindQueueName<Customer>(null).ReturnsForAnyArgs("Queue.Customer");
            _routeFinder.FindRoutingKey<Customer>().Returns("Customer");

            _routeSetupData = new RouteSetupData
            {
                RouteFinder = _routeFinder,
                ExchangeSetupData = new ExchangeSetupData(),
                QueueSetupData = new QueueSetupData
                {
                    AutoExpire = 10000,
                    MessageTimeToLive = 10000000
                }
            };
        }
Beispiel #4
0
        /// <summary>
        /// Delete exchange and queue which suppose to be created by RouteSetupData
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="routeSetupData"></param>
        public virtual void DestroyRoute <T>(RouteSetupData routeSetupData)
        {
            var queueName    = routeSetupData.RouteFinder.FindQueueName <T>(routeSetupData.SubscriptionName);
            var exchangeName = routeSetupData.RouteFinder.FindExchangeName <T>();

            using (var connection = ConnectionFactory.CreateConnection())
            {
                using (var model = connection.CreateModel())
                {
                    // Delete Queue
                    DeleteQueue <T>(model, routeSetupData.QueueSetupData, 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);
                    }
                }
            }
        }
        /// <summary>
        /// Create a new Subscription.
        /// </summary>
        /// <param name="value">The data used to create the subscription</param>
        private static void CreateSubscription(HandlerEnpointData data) {
            //Guard.ArgumentNotNull(value, "value");

            lock (lockObject) {

                var setup = new RabbitSetup(ConfigurationManager.ConnectionStrings["RabbitMQ"].ConnectionString);

                var routeData = new RouteSetupData() {
                    ExchangeSetupData = new ExchangeSetupData() {
                        AutoDelete = false,
                        Durable = true,
                        ExchangeType = "direct"
                    },
                    QueueSetupData = new QueueSetupData() {
                        AutoDelete = false,
                        AutoExpire = 1000 * 60 * 60 * 96, //96 hours
                        Durable = true
                    },
                    SubscriptionName = data.DeclaredType.FullName,
                    RouteFinder = data.RouteFinder
                };

                var methodInfo = setup.GetType().GetMethod("CreateRoute");
                var genericMethodInfo = methodInfo.MakeGenericMethod(new Type[] { data.MessageType });

                genericMethodInfo.Invoke(setup, new object[] { routeData });

                var gt = typeof(RegistrationRunner<>).MakeGenericType(data.MessageType);
                var rr = Activator.CreateInstance(gt, data);

                var configMethod = rr.GetType().GetMethod("Configure");
                configMethod.Invoke(rr, null);

            } //lock end

        }