예제 #1
0
    void UseDirectRoutingTopologyWithCustomConventions(EndpointConfiguration endpointConfiguration)
    {
        #region rabbitmq-config-usedirectroutingtopologywithcustomconventions

        var topology = new DirectRoutingTopology(
            useDurableExchanges: true,
            exchangeNameConvention: () => "MyTopic",
            routingKeyConvention: MyRoutingKeyConvention);

        var transport = new RabbitMQTransport(topology, "host=localhost");

        endpointConfiguration.UseTransport(transport);

        #endregion
    }
        /// <summary>
        /// Use the direct routing topology with the given conventions
        /// </summary>
        /// <param name="routingKeyConvention"></param>
        /// <param name="exchangeNameConvention"></param>
        /// <returns></returns>
        public RabbitMqSettings UseDirectRoutingTopology(Func <Type, string> routingKeyConvention = null, Func <Address, Type, string> exchangeNameConvention = null)
        {
            if (routingKeyConvention == null)
            {
                routingKeyConvention = DefaultRoutingKeyConvention.GenerateRoutingKey;
            }

            if (exchangeNameConvention == null)
            {
                exchangeNameConvention = (address, eventType) => "amq.topic";
            }

            InfrastructureServices.RegisterServiceFor <IRoutingTopology>(() =>
            {
                var router = new DirectRoutingTopology
                {
                    ExchangeNameConvention = exchangeNameConvention,
                    RoutingKeyConvention   = routingKeyConvention
                };

                Configure.Instance.Configurer.RegisterSingleton <IRoutingTopology>(router);
            });
            return(this);
        }
        protected override void Configure(FeatureConfigurationContext context, string connectionString)
        {
            var useCallbackReceiver = context.Settings.Get<bool>(UseCallbackReceiverSettingKey);
            var maxConcurrencyForCallbackReceiver = context.Settings.Get<int>(MaxConcurrencyForCallbackReceiver);
            var queueName = GetLocalAddress(context.Settings);
            var callbackQueue = string.Format("{0}.{1}", queueName, RuntimeEnvironment.MachineName);
            var connectionConfiguration = new ConnectionStringParser(context.Settings).Parse(connectionString);

            MessageConverter messageConverter;

            if (context.Settings.HasSetting(CustomMessageIdStrategy))
            {
                messageConverter = new MessageConverter(context.Settings.Get<Func<BasicDeliverEventArgs, string>>(CustomMessageIdStrategy));
            }
            else
            {
                messageConverter = new MessageConverter();
            }

            string hostDisplayName;
            if (!context.Settings.TryGet("NServiceBus.HostInformation.DisplayName", out hostDisplayName))//this was added in 5.1.2 of the core
            {
                hostDisplayName = RuntimeEnvironment.MachineName;
            }

            var consumerTag = string.Format("{0} - {1}", hostDisplayName, context.Settings.EndpointName());

            var receiveOptions = new ReceiveOptions(workQueue =>
            {
                //if this isn't the main queue we shouldn't use callback receiver
                if (!useCallbackReceiver || workQueue != queueName)
                {
                    return SecondaryReceiveSettings.Disabled();
                }
                return SecondaryReceiveSettings.Enabled(callbackQueue, maxConcurrencyForCallbackReceiver);
            },
            messageConverter,
            connectionConfiguration.PrefetchCount,
            connectionConfiguration.DequeueTimeout * 1000,
            context.Settings.GetOrDefault<bool>("Transport.PurgeOnStartup"),
            consumerTag);



            context.Container.RegisterSingleton(connectionConfiguration);

            context.Container.ConfigureComponent(builder => new RabbitMqDequeueStrategy(
                builder.Build<IManageRabbitMqConnections>(),
                SetupCircuitBreaker(builder.Build<CriticalError>()),
                receiveOptions), DependencyLifecycle.InstancePerCall);


            context.Container.ConfigureComponent<OpenPublishChannelBehavior>(DependencyLifecycle.InstancePerCall);

            context.Pipeline.Register<OpenPublishChannelBehavior.Registration>();

            context.Container.ConfigureComponent<RabbitMqMessageSender>(DependencyLifecycle.InstancePerCall);

            if (useCallbackReceiver)
            {
                context.Container.ConfigureProperty<RabbitMqMessageSender>(p => p.CallbackQueue, callbackQueue);
                context.Container.ConfigureComponent<CallbackQueueCreator>(DependencyLifecycle.InstancePerCall)
                    .ConfigureProperty(p => p.Enabled, true)
                    .ConfigureProperty(p => p.CallbackQueueAddress, Address.Parse(callbackQueue));

                context.Pipeline.Register<ForwardCallbackQueueHeaderBehavior.Registration>();
            }

            context.Container.ConfigureComponent<ChannelProvider>(DependencyLifecycle.InstancePerCall)
                  .ConfigureProperty(p => p.UsePublisherConfirms, connectionConfiguration.UsePublisherConfirms)
                  .ConfigureProperty(p => p.MaxWaitTimeForConfirms, connectionConfiguration.MaxWaitTimeForConfirms);

            context.Container.ConfigureComponent<RabbitMqDequeueStrategy>(DependencyLifecycle.InstancePerCall);
            context.Container.ConfigureComponent<RabbitMqMessagePublisher>(DependencyLifecycle.InstancePerCall);

            context.Container.ConfigureComponent<RabbitMqSubscriptionManager>(DependencyLifecycle.SingleInstance)
             .ConfigureProperty(p => p.EndpointQueueName, queueName);

            context.Container.ConfigureComponent<RabbitMqQueueCreator>(DependencyLifecycle.InstancePerCall);

            if (context.Settings.HasSetting<IRoutingTopology>())
            {
                context.Container.RegisterSingleton(context.Settings.Get<IRoutingTopology>());
            }
            else
            {
                var durable = GetDurableMessagesEnabled(context.Settings);

                IRoutingTopology topology;

                DirectRoutingTopology.Conventions conventions;


                if (context.Settings.TryGet(out conventions))
                {
                    topology = new DirectRoutingTopology(conventions, durable);
                }
                else
                {
                    topology = new ConventionalRoutingTopology(durable);
                }


                context.Container.RegisterSingleton(topology);
            }

            if (context.Settings.HasSetting("IManageRabbitMqConnections"))
            {
                context.Container.ConfigureComponent(context.Settings.Get<Type>("IManageRabbitMqConnections"), DependencyLifecycle.SingleInstance);
            }
            else
            {
                context.Container.ConfigureComponent<RabbitMqConnectionManager>(DependencyLifecycle.SingleInstance);

                context.Container.ConfigureComponent(builder => new RabbitMqConnectionFactory(builder.Build<ConnectionConfiguration>()), DependencyLifecycle.InstancePerCall);
            }
        }
        protected override void Configure(FeatureConfigurationContext context, string connectionString)
        {
            var useCallbackReceiver = context.Settings.Get <bool>(UseCallbackReceiverSettingKey);
            var maxConcurrencyForCallbackReceiver = context.Settings.Get <int>(MaxConcurrencyForCallbackReceiver);
            var queueName               = GetLocalAddress(context.Settings);
            var callbackQueue           = string.Format("{0}.{1}", queueName, RuntimeEnvironment.MachineName);
            var connectionConfiguration = new ConnectionStringParser(context.Settings).Parse(connectionString);

            MessageConverter messageConverter;

            if (context.Settings.HasSetting(CustomMessageIdStrategy))
            {
                messageConverter = new MessageConverter(context.Settings.Get <Func <BasicDeliverEventArgs, string> >(CustomMessageIdStrategy));
            }
            else
            {
                messageConverter = new MessageConverter();
            }

            string hostDisplayName;

            if (!context.Settings.TryGet("NServiceBus.HostInformation.DisplayName", out hostDisplayName))//this was added in 5.1.2 of the core
            {
                hostDisplayName = RuntimeEnvironment.MachineName;
            }

            var consumerTag = string.Format("{0} - {1}", hostDisplayName, context.Settings.EndpointName());

            var receiveOptions = new ReceiveOptions(workQueue =>
            {
                //if this isn't the main queue we shouldn't use callback receiver
                if (!useCallbackReceiver || workQueue != queueName)
                {
                    return(SecondaryReceiveSettings.Disabled());
                }
                return(SecondaryReceiveSettings.Enabled(callbackQueue, maxConcurrencyForCallbackReceiver));
            },
                                                    messageConverter,
                                                    connectionConfiguration.PrefetchCount,
                                                    connectionConfiguration.DequeueTimeout * 1000,
                                                    context.Settings.GetOrDefault <bool>("Transport.PurgeOnStartup"),
                                                    consumerTag);



            context.Container.RegisterSingleton(connectionConfiguration);

            context.Container.ConfigureComponent(builder => new RabbitMqDequeueStrategy(
                                                     builder.Build <IManageRabbitMqConnections>(),
                                                     SetupCircuitBreaker(builder.Build <CriticalError>()),
                                                     receiveOptions), DependencyLifecycle.InstancePerCall);


            context.Container.ConfigureComponent <OpenPublishChannelBehavior>(DependencyLifecycle.InstancePerCall);

            context.Pipeline.Register <OpenPublishChannelBehavior.Registration>();
            context.Pipeline.Register <ReadIncomingCallbackAddressBehavior.Registration>();

            context.Container.ConfigureComponent(b => new RabbitMqMessageSender(b.Build <IRoutingTopology>(), b.Build <IChannelProvider>(), b.Build <PipelineExecutor>().CurrentContext), DependencyLifecycle.InstancePerCall);

            if (useCallbackReceiver)
            {
                context.Container.ConfigureComponent <CallbackQueueCreator>(DependencyLifecycle.InstancePerCall)
                .ConfigureProperty(p => p.Enabled, true)
                .ConfigureProperty(p => p.CallbackQueueAddress, Address.Parse(callbackQueue));

                //context.Pipeline.Register<ForwardCallbackQueueHeaderBehavior.Registration>();
                context.Pipeline.Register <SetOutgoingCallbackAddressBehavior.Registration>();
                context.Container.ConfigureComponent <SetOutgoingCallbackAddressBehavior>(DependencyLifecycle.SingleInstance)
                .ConfigureProperty(p => p.CallbackQueue, callbackQueue);
            }

            context.Container.ConfigureComponent <ChannelProvider>(DependencyLifecycle.InstancePerCall)
            .ConfigureProperty(p => p.UsePublisherConfirms, connectionConfiguration.UsePublisherConfirms)
            .ConfigureProperty(p => p.MaxWaitTimeForConfirms, connectionConfiguration.MaxWaitTimeForConfirms);

            context.Container.ConfigureComponent <RabbitMqDequeueStrategy>(DependencyLifecycle.InstancePerCall);
            context.Container.ConfigureComponent <RabbitMqMessagePublisher>(DependencyLifecycle.InstancePerCall);

            context.Container.ConfigureComponent <RabbitMqSubscriptionManager>(DependencyLifecycle.SingleInstance)
            .ConfigureProperty(p => p.EndpointQueueName, queueName);

            context.Container.ConfigureComponent <RabbitMqQueueCreator>(DependencyLifecycle.InstancePerCall);

            if (context.Settings.HasSetting <IRoutingTopology>())
            {
                context.Container.RegisterSingleton(context.Settings.Get <IRoutingTopology>());
            }
            else
            {
                var durable = GetDurableMessagesEnabled(context.Settings);

                IRoutingTopology topology;

                DirectRoutingTopology.Conventions conventions;


                if (context.Settings.TryGet(out conventions))
                {
                    topology = new DirectRoutingTopology(conventions, durable);
                }
                else
                {
                    topology = new ConventionalRoutingTopology(durable);
                }


                context.Container.RegisterSingleton(topology);
            }

            if (context.Settings.HasSetting("IManageRabbitMqConnections"))
            {
                context.Container.ConfigureComponent(context.Settings.Get <Type>("IManageRabbitMqConnections"), DependencyLifecycle.SingleInstance);
            }
            else
            {
                context.Container.ConfigureComponent <RabbitMqConnectionManager>(DependencyLifecycle.SingleInstance);

                context.Container.ConfigureComponent(builder => new RabbitMqConnectionFactory(builder.Build <ConnectionConfiguration>()), DependencyLifecycle.InstancePerCall);
            }
        }