Class ClientRequestHandler . The RmqMessageProducer is used by a client to talk to a server and abstracts the infrastructure for inter-process communication away from clients. It handles connection establishment, request sending and error handling
Inheritance: MessageGateway, IAmAMessageProducerSupportingDelay
        public static void Run(UnityContainer container)
        {
            container.RegisterType<StoreFrontController>();
            container.RegisterInstance(typeof(ILog), LogProvider.For<StoreService>(), new ContainerControlledLifetimeManager());
            container.RegisterType<AddOrderCommandHandler>();
            container.RegisterType<AddProductCommandHandler>();
            container.RegisterType<ChangeProductCommandHandler>();
            container.RegisterType<RemoveProductCommandHandler>();
            container.RegisterType<IProductsDAO, ProductsDAO>();

            var logger = container.Resolve<ILog>();
            var handlerFactory = new UnityHandlerFactory(container);

            var subscriberRegistry = new SubscriberRegistry();
            subscriberRegistry.Register<AddOrderCommand, AddOrderCommandHandler>();
            subscriberRegistry.Register<AddProductCommand, AddProductCommandHandler>();
            subscriberRegistry.Register<ChangeProductCommand, ChangeProductCommandHandler>();
            subscriberRegistry.Register<RemoveProductCommand, RemoveProductCommandHandler>();

            //create policies
            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.RETRYPOLICY, retryPolicy},
                {CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy}
            };

            var messageMapperFactory = new UnityMessageMapperFactory(container);
            var messageMapperRegistry = new MessageMapperRegistry(messageMapperFactory);
            messageMapperRegistry.Register<AddOrderCommand, AddOrderCommandMessageMapper>();

            var gateway = new RmqMessageProducer(container.Resolve<ILog>());
            var dbPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.Substring(8)), "App_Data\\MessageStore.sdf");

            IAmAMessageStore<Message> sqlMessageStore = new MsSqlMessageStore(new MsSqlMessageStoreConfiguration("DataSource=\"" + dbPath + "\"", "Messages", MsSqlMessageStoreConfiguration.DatabaseType.SqlCe), logger);

            var commandProcessor = CommandProcessorBuilder.With()
                    .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory))
                    .Policies(policyRegistry)
                    .Logger(logger)
                    .TaskQueues(new MessagingConfiguration(sqlMessageStore, gateway, messageMapperRegistry))
                    .RequestContextFactory(new InMemoryRequestContextFactory())
                    .Build();

            container.RegisterInstance(typeof(IAmACommandProcessor), commandProcessor);
        }
Ejemplo n.º 2
0
        private CommandProcessor CommandProcessorRegistrar(TinyIoCContainer container, ILog logger)
        {
            //Database dao
            container.Register<ITasksDAO, TasksDAO>().AsSingleton();

            //create handler 
            var handlerFactory = new TinyIocHandlerFactory(container);
            var subscriberRegistry = new SubscriberRegistry();
            container.Register<IHandleRequests<AddTaskCommand>, AddTaskCommandHandler>().AsMultiInstance();
            subscriberRegistry.Register<AddTaskCommand, AddTaskCommandHandler>();

            //complete handler 
            container.Register<IHandleRequests<CompleteTaskCommand>, CompleteTaskCommandHandler>().AsMultiInstance();
            subscriberRegistry.Register<CompleteTaskCommand, CompleteTaskCommandHandler>();
            
            //create policies
            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.RETRYPOLICY, retryPolicy }, { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy } };

            //create message mappers
            container.Register<IAmAMessageMapper<TaskReminderCommand>, TaskReminderCommandMessageMapper>().AsMultiInstance();
            var messageMapperFactory = new TinyIoCMessageMapperFactory(container);
            var messageMapperRegistry = new MessageMapperRegistry(messageMapperFactory);
            messageMapperRegistry.Add(typeof(TaskReminderCommand), typeof(TaskReminderCommandMessageMapper));
           
            var gateway = new RmqMessageProducer(logger);
            IAmAMessageStore<Message> sqlMessageStore = new MsSqlMessageStore(new MsSqlMessageStoreConfiguration("Server=.;Database=brighterMessageStore;Trusted_Connection=True", "messages", MsSqlMessageStoreConfiguration.DatabaseType.MsSqlServer), logger);
            var commandProcessor =
                CommandProcessorBuilder.With()
                    .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory))
                    .Policies(policyRegistry)
                    .Logger(logger)
                    .TaskQueues(new MessagingConfiguration(sqlMessageStore, gateway, messageMapperRegistry))
                    .RequestContextFactory(new InMemoryRequestContextFactory())
                    .Build();

            return commandProcessor;
        }
Ejemplo n.º 3
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
           // Add framework serviceProvider.
            services.AddMvc();

            services.AddSingleton<ITaskListRetriever, TaskListRetriever>();
            services.AddSingleton<ITaskRetriever, TaskRetriever>();
            services.AddSingleton<ITasksDAO, TasksDAO>();

            //create handler 

            var subscriberRegistry = new SubscriberRegistry();
            services.AddTransient<IHandleRequests<AddTaskCommand>, AddTaskCommandHandler>();
            subscriberRegistry.Register<AddTaskCommand, AddTaskCommandHandler>();

            //complete handler 
            services.AddTransient<IHandleRequests<CompleteTaskCommand>, CompleteTaskCommandHandler>();
            subscriberRegistry.Register<CompleteTaskCommand, CompleteTaskCommandHandler>();

            //create policies
            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.RETRYPOLICY, retryPolicy }, { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy } };

            //create message mappers
            services.AddTransient<IAmAMessageMapper<TaskReminderCommand>, TaskReminderCommandMessageMapper>();
            
            var messagingGatewayConfiguration = RmqGatewayBuilder.With.Uri(new Uri(Configuration["RabbitMQ:Uri"])).Exchange(Configuration["RabbitMQ:Exchange"]).DefaultQueues();

            var gateway = new RmqMessageProducer(messagingGatewayConfiguration);
            IAmAMessageStore<Message> sqlMessageStore = new SqliteMessageStore(new SqliteMessageStoreConfiguration("Data Source = tasks.db", "MessageStores"));

            var container = new Container();
            container.Configure(config =>
            {
                var servicesMessageMapperFactory = new ServicesMessageMapperFactory(container);

                var messageMapperRegistry = new MessageMapperRegistry(servicesMessageMapperFactory)
                    {
                        {typeof(TaskReminderCommand), typeof(TaskReminderCommandMessageMapper)},
                        {typeof(TaskAddedEvent), typeof(TaskAddedEventMapper)},
                        {typeof(TaskEditedEvent), typeof(TaskEditedEventMapper)},
                        {typeof(TaskCompletedEvent), typeof(TaskCompletedEventMapper)},
                        {typeof(TaskReminderSentEvent), typeof(TaskReminderSentEventMapper)}
                    };

                var servicesHandlerFactory = new ServicesHandlerFactory(container);

                var commandProcessor = CommandProcessorBuilder.With()
                    .Handlers(new HandlerConfiguration(subscriberRegistry, servicesHandlerFactory))
                    .Policies(policyRegistry)
                    .TaskQueues(new MessagingConfiguration(sqlMessageStore, gateway, messageMapperRegistry))
                    .RequestContextFactory(new InMemoryRequestContextFactory())
                    .Build();

                config.For<IAmACommandProcessor>().Singleton().Use(() => commandProcessor);

            });
            container.Populate(services);

            return container.GetInstance<IServiceProvider>();
        }