Example #1
0
        protected override ConfigurationElement CreateNewElement()
        {
            var newElement = new ConnectionElement();

            elements.Add(newElement);
            return(newElement);
        }
        /// <summary>
        /// Builds this instance.
        /// </summary>
        /// <param name="hostName">Name of the host.</param>
        /// <returns>Dispatcher.</returns>
        public Dispatcher Build(string hostName)
        {
            var connections = new List<ConnectionElement>();

            /* 
             * These are the control bus channels, we hardcode them because we want to know they exist, but we use
            a base naming scheme to allow centralized management.
             */

            var configurationElement = new ConnectionElement
            {
                ChannelName = CONFIGURATION,
                ConnectionName = CONFIGURATION,
                IsDurable = true,
                DataType = typeof(ConfigurationCommand).FullName,
                RoutingKey = hostName + "." + CONFIGURATION,
            };
            connections.Add(configurationElement);

            var heartbeatElement = new ConnectionElement
            {
                ChannelName = HEARTBEAT,
                ConnectionName = HEARTBEAT,
                IsDurable = false,
                DataType = typeof(HeartBeatCommand).FullName,
                RoutingKey = hostName + "." + HEARTBEAT,
            };
            connections.Add(heartbeatElement);

            /* We want to register policies, messages and handlers for receiving built in commands. It's simple enough to do this for
             the registries, but we cannot know your HandlerFactory implementation in order to insert. So we have to rely on
             an internal HandlerFactory to build these for you.
             
             * We also need to  pass the supervised dispatcher as a dependency to our command handlers, so this allows us to manage
             * the injection of the dependency as part of our handler factory
             
             */
            
            var retryPolicy = Policy
                .Handle<Exception>()
                .WaitAndRetry(new[]
                {
                    TimeSpan.FromMilliseconds(50),
                    TimeSpan.FromMilliseconds(100),
                    TimeSpan.FromMilliseconds(150)
                });

            var circuitBreakerPolicy = Policy
                .Handle<Exception>()
                .CircuitBreaker(1, TimeSpan.FromMilliseconds(500));

            var policyRegistry = new PolicyRegistry()
            {
                {CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy},
                {CommandProcessor.RETRYPOLICY, retryPolicy}
            };


            var subscriberRegistry = new SubscriberRegistry();
            subscriberRegistry.Register<ConfigurationCommand, ConfigurationCommandHandler>();
            
            var messageMapperRegistry = new MessageMapperRegistry(new ControlBusMessageMapperFactory());
            messageMapperRegistry.Register<ConfigurationCommand, ConfigurationCommandMessageMapper>();

            var commandProcessor = CommandProcessorBuilder.With()
                .Handlers(new HandlerConfiguration(subscriberRegistry, new ControlBusHandlerFactory(_dispatcher, _logger)))
                .Policies(policyRegistry)
                .NoTaskQueues()
                .RequestContextFactory(new InMemoryRequestContextFactory())
                .Build();

            return DispatchBuilder
                .With()
                .CommandProcessor(commandProcessor)
                .MessageMappers(messageMapperRegistry)
                .ChannelFactory(_channelFactory)
                .ConnectionsFromElements(connections)
                .Build();
        }
 protected override ConfigurationElement CreateNewElement()
 {
     var newElement = new ConnectionElement();
     _elements.Add(newElement);
     return newElement;
 }