Exemplo n.º 1
0
        internal static Bus Build(BusBuilderConfiguration configuration)
        {
            var logger = configuration.Logger;

            logger.Debug("Constructing bus...");

            configuration.AssertConfigurationIsValid();

            var container = new PoorMansIoC();

            container.RegisterPropertiesFromConfigurationObject(configuration);

            logger.Debug("Creating message pumps...");
            var messagePumpsManager = new MessagePumpsManager(
                container.Resolve <ResponseMessagePumpFactory>().Create(),
                container.Resolve <RequestMessagePumpsFactory>().CreateAll(),
                container.Resolve <CommandMessagePumpsFactory>().CreateAll(),
                container.Resolve <MulticastRequestMessagePumpsFactory>().CreateAll(),
                container.Resolve <MulticastEventMessagePumpsFactory>().CreateAll(),
                container.Resolve <CompetingEventMessagePumpsFactory>().CreateAll());

            logger.Debug("Message pumps are all created.");

            var bus = container.ResolveWithOverrides <Bus>(messagePumpsManager);

            container.Resolve <PropertyInjector>().Bus = bus;

            bus.Starting += (s, e) =>
            {
                Task.Run(async() =>
                {
                    try
                    {
                        await container.Resolve <INimbusTransport>().TestConnection();
                        await CleanNamespace(container, logger);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex, "Failed to establish connection to underlying transport: {Message}", ex.Message);
                        throw;
                    }
                }).Wait();
            };

            bus.Disposing += (s, e) =>
            {
                Task.Run(async() =>
                {
                    await CleanNamespace(container, logger);
                    container.Dispose();
                }).Wait();
            };

            logger.Info("Bus built. Job done!");

            return(bus);
        }
Exemplo n.º 2
0
        private static void RegisterPropertiesFromConfigurationObject(PoorMansIoC container, object configuration)
        {
            foreach (var prop in configuration.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
            {
                var value = prop.GetValue(configuration);
                if (value == null)
                {
                    continue;
                }

                container.Register(value);
            }
        }
Exemplo n.º 3
0
        private static async Task CleanNamespace(PoorMansIoC container, ILogger logger)
        {
            var removeAllExistingElements = container.Resolve <RemoveAllExistingNamespaceElementsSetting>();

            if (!removeAllExistingElements)
            {
                return;
            }

            logger.Debug("Removing all existing namespace elements. IMPORTANT: This should only be done in your regression test suites.");
            var cleanser = container.Resolve <INamespaceCleanser>();
            await cleanser.RemoveAllExistingNamespaceElements();
        }
Exemplo n.º 4
0
        internal static Bus Build(BusBuilderConfiguration configuration)
        {
            var logger = configuration.Logger;

            logger.Debug("Constructing bus...");

            configuration.AssertConfigurationIsValid();

            var container = new PoorMansIoC();

            container.RegisterPropertiesFromConfigurationObject(configuration);

            logger.Debug("Creating message pumps...");
            var messagePumpsManager = new MessagePumpsManager(
                container.Resolve <ResponseMessagePumpFactory>().Create(),
                container.Resolve <RequestMessagePumpsFactory>().CreateAll(),
                container.Resolve <CommandMessagePumpsFactory>().CreateAll(),
                container.Resolve <MulticastRequestMessagePumpsFactory>().CreateAll(),
                container.Resolve <MulticastEventMessagePumpsFactory>().CreateAll(),
                container.Resolve <CompetingEventMessagePumpsFactory>().CreateAll());

            logger.Debug("Message pumps are all created.");

            var bus = container.ResolveWithOverrides <Bus>(messagePumpsManager);

            container.Resolve <PropertyInjector>().Bus = bus;

            bus.Starting += delegate
            {
                container.Resolve <INimbusTransport>().TestConnection().Wait();

                var removeAllExistingElements = container.Resolve <RemoveAllExistingNamespaceElementsSetting>();
                if (removeAllExistingElements)
                {
                    logger.Debug("Removing all existing namespace elements. IMPORTANT: This should only be done in your regression test suites.");
                    var cleanser = container.Resolve <INamespaceCleanser>();
                    cleanser.RemoveAllExistingNamespaceElements().Wait();
                }
            };
            bus.Disposing += delegate { container.Dispose(); };

            logger.Info("Bus built. Job done!");

            return(bus);
        }
        internal static void RegisterPropertiesFromConfigurationObject(this PoorMansIoC container, INimbusConfiguration configuration)
        {
            configuration.RegisterWith(container);

            configuration
            .GetType()
            .GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
            .Where(p => typeof(ISetting).IsAssignableFrom(p.PropertyType))
            .Select(p => p.GetValue(configuration))
            .Do(o => container.Register(o))
            .Done();

            configuration
            .GetType()
            .GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
            .Where(p => typeof(INimbusConfiguration).IsAssignableFrom(p.PropertyType))
            .Select(p => (INimbusConfiguration)p.GetValue(configuration))
            .Do(container.RegisterPropertiesFromConfigurationObject)
            .Done();
        }
Exemplo n.º 6
0
        public void RegisterWith(PoorMansIoC container)
        {
            container.Register(TypeProvider, typeof(ITypeProvider));
            container.Register(DependencyResolver, typeof(IDependencyResolver));
            container.Register(Logger, typeof(ILogger));
            container.Register(Serializer, typeof(ISerializer));
            container.Register(Compressor, typeof(ICompressor));
            container.Register(Router, typeof(IRouter));
            container.Register(DeliveryRetryStrategy, typeof(IDeliveryRetryStrategy));

            container.RegisterType <PathFactory>(ComponentLifetime.SingleInstance, typeof(IPathFactory));
            container.RegisterType <ReplyQueueNameSetting>(ComponentLifetime.SingleInstance);
            container.RegisterType <RequestResponseCorrelator>(ComponentLifetime.SingleInstance);
            container.RegisterType <CommandMessagePumpsFactory>(ComponentLifetime.SingleInstance);
            container.RegisterType <RequestMessagePumpsFactory>(ComponentLifetime.SingleInstance);
            container.RegisterType <ResponseMessagePumpFactory>(ComponentLifetime.SingleInstance);
            container.RegisterType <MulticastRequestMessagePumpsFactory>(ComponentLifetime.SingleInstance);
            container.RegisterType <MulticastEventMessagePumpsFactory>(ComponentLifetime.SingleInstance);
            container.RegisterType <CompetingEventMessagePumpsFactory>(ComponentLifetime.SingleInstance);
            container.RegisterType <SystemClock>(ComponentLifetime.SingleInstance, typeof(IClock));
            container.RegisterType <DispatchContextManager>(ComponentLifetime.SingleInstance, typeof(IDispatchContextManager));
            container.RegisterType <ResponseMessageDispatcher>(ComponentLifetime.SingleInstance);
            container.RegisterType <HandlerMapper>(ComponentLifetime.SingleInstance, typeof(IHandlerMapper));
            container.RegisterType <MessageDispatcherFactory>(ComponentLifetime.SingleInstance, typeof(IMessageDispatcherFactory));
            container.RegisterType <InboundInterceptorFactory>(ComponentLifetime.SingleInstance, typeof(IInboundInterceptorFactory));
            container.RegisterType <OutboundInterceptorFactory>(ComponentLifetime.SingleInstance, typeof(IOutboundInterceptorFactory));
            container.RegisterType <PropertyInjector>(ComponentLifetime.SingleInstance, typeof(IPropertyInjector), typeof(PropertyInjector));
            container.RegisterType <NimbusMessageFactory>(ComponentLifetime.SingleInstance, typeof(INimbusMessageFactory));
            container.RegisterType <BusCommandSender>(ComponentLifetime.SingleInstance, typeof(ICommandSender));
            container.RegisterType <BusRequestSender>(ComponentLifetime.SingleInstance, typeof(IRequestSender));
            container.RegisterType <BusMulticastRequestSender>(ComponentLifetime.SingleInstance, typeof(IMulticastRequestSender));
            container.RegisterType <BusEventSender>(ComponentLifetime.SingleInstance, typeof(IEventSender));
            container.RegisterType <KnownMessageTypeVerifier>(ComponentLifetime.SingleInstance, typeof(IKnownMessageTypeVerifier));
            container.RegisterType <Heartbeat>(ComponentLifetime.SingleInstance, typeof(IHeartbeat));
            container.RegisterType <Bus>(ComponentLifetime.SingleInstance);
            container.RegisterType <GlobalHandlerThrottle>(ComponentLifetime.SingleInstance, typeof(IGlobalHandlerThrottle));
            container.RegisterType <FilterConditionProvider>(ComponentLifetime.SingleInstance, typeof(IFilterConditionProvider));

            container.RegisterType <MessagePump>(ComponentLifetime.InstancePerDependency);
            container.RegisterType <DefaultRetry>(ComponentLifetime.InstancePerDependency, typeof(IRetry));
        }
Exemplo n.º 7
0
        internal static Bus Build(BusBuilderConfiguration configuration)
        {
            var logger = configuration.Logger;

            logger.Debug("Constructing bus...");

            var container = new PoorMansIoC();

            RegisterPropertiesFromConfigurationObject(container, configuration);
            RegisterPropertiesFromConfigurationObject(container, configuration.LargeMessageStorageConfiguration);
            RegisterPropertiesFromConfigurationObject(container, configuration.Debugging);

            var namespaceManagerRoundRobin = new RoundRobin <NamespaceManager>(
                container.Resolve <ServerConnectionCountSetting>(),
                () =>
            {
                var namespaceManager = NamespaceManager.CreateFromConnectionString(container.Resolve <ConnectionStringSetting>());
                namespaceManager.Settings.OperationTimeout = TimeSpan.FromSeconds(120);
                return(namespaceManager);
            },
                nsm => false,
                nsm => { });

            container.Register <Func <NamespaceManager> >(c => namespaceManagerRoundRobin.GetNext);

            var messagingFactoryRoundRobin = new RoundRobin <MessagingFactory>(
                container.Resolve <ServerConnectionCountSetting>(),
                () =>
            {
                var messagingFactory           = MessagingFactory.CreateFromConnectionString(container.Resolve <ConnectionStringSetting>());
                messagingFactory.PrefetchCount = container.Resolve <ConcurrentHandlerLimitSetting>();
                return(messagingFactory);
            },
                mf => mf.IsBorked(),
                mf => { });

            container.Register <Func <MessagingFactory> >(c => messagingFactoryRoundRobin.GetNext);

            if (configuration.Debugging.RemoveAllExistingNamespaceElements)
            {
                var namespaceCleanser = container.Resolve <NamespaceCleanser>();
                namespaceCleanser.RemoveAllExistingNamespaceElements().Wait();
            }

            logger.Debug("Creating message pumps...");

            var messagePumps = new MessagePumpsManager(
                container.Resolve <ResponseMessagePumpFactory>().Create(),
                container.Resolve <RequestMessagePumpsFactory>().CreateAll(),
                container.Resolve <CommandMessagePumpsFactory>().CreateAll(),
                container.Resolve <MulticastRequestMessagePumpsFactory>().CreateAll(),
                container.Resolve <MulticastEventMessagePumpsFactory>().CreateAll(),
                container.Resolve <CompetingEventMessagePumpsFactory>().CreateAll(),
                container.Resolve <INimbusTaskFactory>());

            logger.Debug("Message pumps are all created.");

            var bus = new Bus(container.Resolve <ILogger>(),
                              container.Resolve <ICommandSender>(),
                              container.Resolve <IRequestSender>(),
                              container.Resolve <IMulticastRequestSender>(),
                              container.Resolve <IEventSender>(),
                              messagePumps,
                              container.Resolve <DeadLetterQueues>(),
                              container.Resolve <INimbusTaskFactory>(),
                              container.Resolve <IHeartbeat>());

            bus.Starting += delegate
            {
                container.Resolve <AzureQueueManager>().WarmUp();
                container.Resolve <PropertyInjector>().Bus = bus;
            };
            bus.Disposing += delegate { container.Dispose(); };

            logger.Info("Bus built. Job done!");

            return(bus);
        }