Ejemplo n.º 1
0
        /// <summary>
        /// Конфигурирует клиента шины сообщений.
        /// </summary>
        /// <param name="endpointName">
        /// Имя точки подключения к шине.
        /// </param>
        /// <param name="cfg">
        /// Конфигуратор клиента шины.
        /// </param>
        /// <returns>
        /// Конфигуратор клиента шины, после применения к нему всех настроек.
        /// </returns>
        public IBusConfigurator Configure(string endpointName, IBusConfigurator cfg)
        {
            if (cfg == null)
            {
                throw new ArgumentNullException("cfg", "Файл конфигурации не может быть null");
            }

            var endpointConfig = this.GetEndPointByName(endpointName);

            IConnectionStringProvider connectionStringProvider = null;

            if (!string.IsNullOrEmpty(endpointConfig.ConnectionStringProvider))
            {
                connectionStringProvider = this.GetConnectionStringProvider(endpointConfig.ConnectionStringProvider);
            }

            cfg.SetEndpoint(endpointConfig.Name);
            cfg.SetConnectionString(endpointConfig.ConnectionString);
            cfg.SetExcludedIncomingHeaders(endpointConfig.ExcludedHeaders);

            if (endpointConfig.ReuseConnection.HasValue)
            {
                cfg.ReuseConnection(endpointConfig.ReuseConnection.Value);
            }

            if (!string.IsNullOrWhiteSpace(endpointConfig.LifecycleHandler))
            {
                cfg.HandleLifecycleWith(this.ResolveLifecycleHandler(endpointConfig.LifecycleHandler));
            }

            if (endpointConfig.ParallelismLevel.HasValue)
            {
                cfg.UseParallelismLevel(endpointConfig.ParallelismLevel.Value);
            }

            if (endpointConfig.FaultQueueTtl.HasValue)
            {
                cfg.UseFaultQueueTtl(endpointConfig.FaultQueueTtl.Value);
            }

            if (endpointConfig.FaultQueueLimit.HasValue)
            {
                cfg.UseFaultQueueLimit(endpointConfig.FaultQueueLimit.Value);
            }

            if (endpointConfig.Dynamic != null)
            {
                if (endpointConfig.Dynamic.Outgoing.HasValue)
                {
                    if (endpointConfig.Dynamic.Outgoing.Value)
                    {
                        cfg.Route(MessageLabel.Any)
                        .ConfiguredWith(
                            builder => new LambdaRouteResolver(
                                (endpoint, label) =>
                        {
                            builder.Topology.Declare(Exchange.Named(label.Name).Durable.Fanout);
                            return(new RabbitRoute(label.Name));
                        }));
                    }
                }
            }

            if (endpointConfig.Qos != null)
            {
                if (endpointConfig.Qos.PrefetchCount.HasValue)
                {
                    cfg.SetDefaultQoS(endpointConfig.Qos.PrefetchCount.Value);
                }
            }

            foreach (ValidatorElement validator in endpointConfig.Validators)
            {
                if (validator.Group)
                {
                    MessageValidatorGroup v = this.ResolveValidatorGroup(validator.Name);
                    cfg.RegisterValidators(v);
                }
                else
                {
                    IMessageValidator v = this.ResolveValidator(validator.Name);
                    cfg.RegisterValidator(v);
                }
            }

            foreach (OutgoingElement outgoingElement in endpointConfig.Outgoing)
            {
                var configurator = cfg.Route(outgoingElement.Label).WithAlias(outgoingElement.Key);

                if (outgoingElement.Confirm)
                {
                    configurator.WithConfirmation();
                }

                if (outgoingElement.Persist)
                {
                    configurator.Persistently();
                }

                if (outgoingElement.Ttl.HasValue)
                {
                    configurator.WithTtl(outgoingElement.Ttl.Value);
                }

                if (outgoingElement.CallbackEndpoint.Default)
                {
                    configurator.WithDefaultCallbackEndpoint();
                }

                if (outgoingElement.Timeout.HasValue)
                {
                    configurator.WithRequestTimeout(outgoingElement.Timeout);
                }

                if (outgoingElement.Delayed || outgoingElement.Delay.HasValue || endpointConfig.Delayed)
                {
                    configurator.WithDelay(outgoingElement.Delay ?? TimeSpan.Zero);
                }

                // Connection string
                var connectionString = endpointConfig.ConnectionString;
                if (!string.IsNullOrEmpty(outgoingElement.ConnectionString))
                {
                    connectionString = outgoingElement.ConnectionString;
                }

                connectionString = connectionStringProvider?.GetConnectionString(outgoingElement.Label.ToMessageLabel()) ?? connectionString;


                configurator.WithConnectionString(connectionString);

                // Reuse connection
                if (outgoingElement.ReuseConnection.HasValue)
                {
                    configurator.ReuseConnection(outgoingElement.ReuseConnection.Value);
                }
            }

            foreach (IncomingElement incomingElement in endpointConfig.Incoming)
            {
                var configurator = cfg.On(incomingElement.Label).WithAlias(incomingElement.Key);

                uint   size  = 0;
                ushort count = 50;

                // This should be the default values provided by RabbitMQ configurator (BusConsumerConfigurationEx);
                var qos = configurator.GetQoS();
                if (qos.HasValue)
                {
                    size  = qos.Value.PrefetchSize;
                    count = qos.Value.PrefetchCount;
                }

                // Prefetch size
                if (endpointConfig.Qos.PrefetchSize.HasValue)
                {
                    size = endpointConfig.Qos.PrefetchSize.Value;
                }

                if (incomingElement.Qos.PrefetchSize.HasValue)
                {
                    size = incomingElement.Qos.PrefetchSize.Value;
                }

                // Prefetch count
                if (endpointConfig.Qos.PrefetchCount.HasValue)
                {
                    count = endpointConfig.Qos.PrefetchCount.Value;
                }

                if (incomingElement.Qos.PrefetchCount.HasValue)
                {
                    count = incomingElement.Qos.PrefetchCount.Value;
                }

                configurator.WithQoS(new QoSParams(count, size));

                // Parallelism level
                if (endpointConfig.ParallelismLevel.HasValue)
                {
                    configurator.WithParallelismLevel(endpointConfig.ParallelismLevel.Value);
                }

                if (incomingElement.ParallelismLevel.HasValue)
                {
                    configurator.WithParallelismLevel(incomingElement.ParallelismLevel.Value);
                }

                // Accept
                if (incomingElement.RequiresAccept)
                {
                    configurator.RequiresAccept();
                }

                if (incomingElement.Delayed)
                {
                    configurator.Delayed();
                }

                // Connection string
                var connectionString = endpointConfig.ConnectionString;
                if (!string.IsNullOrEmpty(incomingElement.ConnectionString))
                {
                    connectionString = incomingElement.ConnectionString;
                }
                connectionString = connectionStringProvider?.GetConnectionString(incomingElement.Label.ToMessageLabel()) ?? connectionString;


                configurator.WithConnectionString(connectionString);

                // Reuse connection
                if (incomingElement.ReuseConnection.HasValue)
                {
                    configurator.ReuseConnection(incomingElement.ReuseConnection.Value);
                }

                Type messageType = typeof(ExpandoObject);
                if (!string.IsNullOrWhiteSpace(incomingElement.Type))
                {
                    messageType = ResolveType(incomingElement.Type);
                }

                var consumerFactory = this.BuildConsumerFactory(incomingElement, messageType, endpointName);

                object consumer = BuildConsumer(consumerFactory, messageType, incomingElement.Lifestyle);

                RegisterConsumer(configurator, messageType, consumer);

                if (!string.IsNullOrWhiteSpace(incomingElement.Validate))
                {
                    IMessageValidator validator = this.ResolveValidator(incomingElement.Validate, messageType);

                    configurator.WhenVerifiedBy(validator);
                }
            }

            var collectorType = endpointConfig.Metrics?.Collector;

            if (!string.IsNullOrEmpty(collectorType))
            {
                try
                {
                    var metricsCollector = (IMetricsCollector)this.dependencyResolver.Resolve(collectorType, typeof(IMetricsCollector));
                    cfg.CollectMetrics(metricsCollector);
                }
                catch (Exception e)
                {
                    Log.Warn(m => m("Could not load metric collector '{0}'", collectorType), e);
                }
            }
            else
            {
                Log.Trace(m => m("Metric collector is not specified"));
            }

            foreach (var configurator in this.childConfigurators)
            {
                configurator.Configure(endpointName, cfg);
            }

            return(cfg);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Конфигурирует клиента шины сообщений.
        /// </summary>
        /// <param name="endpointName">
        /// Имя точки подключения к шине.
        /// </param>
        /// <param name="cfg">
        /// Конфигуратор клиента шины.
        /// </param>
        /// <returns>
        /// Конфигуратор клиента шины, после применения к нему всех настроек.
        /// </returns>
        public IBusConfigurator Configure(string endpointName, IBusConfigurator cfg)
        {
            if (cfg == null)
            {
                throw new ArgumentNullException("cfg", "Файл конфигурации не может быть null");
            }

            EndpointElement endpointConfig = this.GetEndPointByName(endpointName);

            cfg.SetEndpoint(endpointConfig.Name);

            cfg.SetConnectionString(endpointConfig.ConnectionString);

            if (!string.IsNullOrWhiteSpace(endpointConfig.LifecycleHandler))
            {
                cfg.HandleLifecycleWith(this.ResolveLifecycleHandler(endpointConfig.LifecycleHandler));
            }

            if (endpointConfig.Caching != null && endpointConfig.Caching.Enabled)
            {
                cfg.EnableCaching();
            }

            if (endpointConfig.ParallelismLevel.HasValue)
            {
                cfg.UseParallelismLevel(endpointConfig.ParallelismLevel.Value);
            }

            if (endpointConfig.Dynamic != null)
            {
                if (endpointConfig.Dynamic.Outgoing.HasValue)
                {
                    if (endpointConfig.Dynamic.Outgoing.Value)
                    {
                        cfg.Route(MessageLabel.Any)
                        .ConfiguredWith(builder => new LambdaRouteResolver(
                                            (endpoint, label) =>
                        {
                            builder.Topology.Declare(
                                Exchange.Named(label.Name)
                                .Durable.Fanout);
                            return(new RabbitRoute(label.Name));
                        }));
                    }
                }
            }

            if (endpointConfig.Qos != null)
            {
                if (endpointConfig.Qos.PrefetchCount.HasValue)
                {
                    cfg.SetDefaultQoS(endpointConfig.Qos.PrefetchCount.Value);
                }
            }

            foreach (ValidatorElement validator in endpointConfig.Validators)
            {
                if (validator.Group)
                {
                    MessageValidatorGroup v = this.ResolveValidatorGroup(validator.Name);
                    cfg.RegisterValidators(v);
                }
                else
                {
                    IMessageValidator v = this.ResolveValidator(validator.Name);
                    cfg.RegisterValidator(v);
                }
            }

            foreach (OutgoingElement message in endpointConfig.Outgoing)
            {
                ISenderConfigurator senderCfg = cfg.Route(message.Label).
                                                WithAlias(message.Key);

                if (message.Confirm)
                {
                    senderCfg.WithConfirmation();
                }

                if (message.Persist)
                {
                    senderCfg.Persistently();
                }

                if (message.Ttl.HasValue)
                {
                    senderCfg.WithTtl(message.Ttl.Value);
                }

                if (message.CallbackEndpoint.Default)
                {
                    senderCfg.WithDefaultCallbackEndpoint();
                }

                if (message.Timeout.HasValue)
                {
                    senderCfg.WithRequestTimeout(message.Timeout);
                }
            }

            foreach (IncomingElement message in endpointConfig.Incoming)
            {
                IReceiverConfigurator receiver = cfg.On(message.Label).
                                                 WithAlias(message.Key);

                if (message.RequiresAccept)
                {
                    receiver.RequiresAccept();
                }

                Type messageType = typeof(ExpandoObject);
                if (!string.IsNullOrWhiteSpace(message.Type))
                {
                    messageType = ResolveType(message.Type);
                }

                var consumerFactory = this.BuildConsumerFactory(message.React, messageType);

                object consumer = BuildConsumer(consumerFactory, messageType, message.Lifestyle);

                RegisterConsumer(receiver, messageType, consumer);

                if (!string.IsNullOrWhiteSpace(message.Validate))
                {
                    IMessageValidator validator = this.ResolveValidator(message.Validate, messageType);

                    receiver.WhenVerifiedBy(validator);
                }
            }

            return(cfg);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Конфигурирует клиента шины сообщений.
        /// </summary>
        /// <param name="endpointName">
        /// Имя точки подключения к шине.
        /// </param>
        /// <param name="cfg">
        /// Конфигуратор клиента шины.
        /// </param>
        /// <returns>
        /// Конфигуратор клиента шины, после применения к нему всех настроек.
        /// </returns>
        public IBusConfigurator Configure(string endpointName, IBusConfigurator cfg)
        {
            if (cfg == null)
            {
                throw new ArgumentNullException("cfg", "Файл конфигурации не может быть null");
            }

            EndpointElement endpointConfig = this.GetEndPointByName(endpointName);

            cfg.SetEndpoint(endpointConfig.Name);

            cfg.SetConnectionString(endpointConfig.ConnectionString);

            if (!string.IsNullOrWhiteSpace(endpointConfig.LifecycleHandler))
            {
                cfg.HandleLifecycleWith(this.ResolveLifecycleHandler(endpointConfig.LifecycleHandler));
            }

            if (endpointConfig.Caching != null && endpointConfig.Caching.Enabled)
            {
                cfg.EnableCaching();
            }

            if (endpointConfig.ParallelismLevel.HasValue)
            {
                cfg.UseParallelismLevel(endpointConfig.ParallelismLevel.Value);
            }

            if (endpointConfig.Dynamic != null)
            {
                if (endpointConfig.Dynamic.Outgoing.HasValue)
                {
                    if (endpointConfig.Dynamic.Outgoing.Value)
                    {
                        cfg.Route(MessageLabel.Any)
                            .ConfiguredWith(builder => new LambdaRouteResolver(
                                    (endpoint, label) =>
                                        {
                                            builder.Topology.Declare(
                                                Exchange.Named(label.Name)
                                                    .Durable.Fanout);
                                            return new RabbitRoute(label.Name);
                                        }));
                    }
                }
            }

            if (endpointConfig.Qos != null)
            {
                if (endpointConfig.Qos.PrefetchCount.HasValue)
                {
                    cfg.SetDefaultQoS(endpointConfig.Qos.PrefetchCount.Value);
                }
            }

            foreach (ValidatorElement validator in endpointConfig.Validators)
            {
                if (validator.Group)
                {
                    MessageValidatorGroup v = this.ResolveValidatorGroup(validator.Name);
                    cfg.RegisterValidators(v);
                }
                else
                {
                    IMessageValidator v = this.ResolveValidator(validator.Name);
                    cfg.RegisterValidator(v);
                }
            }

            foreach (OutgoingElement message in endpointConfig.Outgoing)
            {
                ISenderConfigurator senderCfg = cfg.Route(message.Label).
                    WithAlias(message.Key);

                if (message.Confirm)
                {
                    senderCfg.WithConfirmation();
                }

                if (message.Persist)
                {
                    senderCfg.Persistently();
                }

                if (message.Ttl.HasValue)
                {
                    senderCfg.WithTtl(message.Ttl.Value);
                }

                if (message.CallbackEndpoint.Default)
                {
                    senderCfg.WithDefaultCallbackEndpoint();
                }

                if (message.Timeout.HasValue)
                {
                    senderCfg.WithRequestTimeout(message.Timeout);
                }
            }

            foreach (IncomingElement message in endpointConfig.Incoming)
            {
                IReceiverConfigurator receiver = cfg.On(message.Label).
                    WithAlias(message.Key);

                if (message.RequiresAccept)
                {
                    receiver.RequiresAccept();
                }

                Type messageType = typeof(ExpandoObject);
                if (!string.IsNullOrWhiteSpace(message.Type))
                {
                    messageType = ResolveType(message.Type);
                }

                var consumerFactory = this.BuildConsumerFactory(message.React, messageType);

                object consumer = BuildConsumer(consumerFactory, messageType, message.Lifestyle);

                RegisterConsumer(receiver, messageType, consumer);

                if (!string.IsNullOrWhiteSpace(message.Validate))
                {
                    IMessageValidator validator = this.ResolveValidator(message.Validate, messageType);

                    receiver.WhenVerifiedBy(validator);
                }
            }

            return cfg;
        }