Ejemplo n.º 1
0
        public TaskMailerService()
        {
            //Create a logger
            var properties = new NameValueCollection();
            properties["showDateTime"] = "true";
            LogManager.Adapter = new ConsoleOutLoggerFactoryAdapter(properties);
            var logger = LogManager.GetLogger(typeof (Dispatcher));
                
            var container = new TinyIoCContainer();
            container.Register<IAmAMessageMapper<TaskReminderCommand>, TaskReminderCommandMessageMapper>();
            var handlerFactory = new TinyIocHandlerFactory(container);

            var subscriberRegistry = new SubscriberRegistry();

            //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
            //var messageMapperRegistry = 

            //create the gateway
            var gateway = new RMQMessagingGateway(logger);

            var builder = DispatchBuilder.With()
                        .WithLogger(logger)
                        .WithCommandProcessor(CommandProcessorBuilder.With()
                            .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory))
                            .Policies(policyRegistry)
                            .Logger(logger)
                            .TaskQueues(new MessagingConfiguration(
                                messageStore: new RavenMessageStore(new EmbeddableDocumentStore(), logger),
                                messagingGateway: gateway,
                                messageMapperRegistry: null
                                ))
                            .RequestContextFactory(new InMemoryRequestContextFactory())
                            .Build()
                            )
                            .WithMessageMappers(null)
                            .WithChannelFactory(new RMQInputChannelfactory(gateway)) 
                            .ConnectionsFromConfiguration();

            dispatcher = builder.Build();

        }
        private PipelineContinuation InitializeContainer(ICommunicationContext arg)
        {
            var container = new TinyIoCContainer();
            container.Register<IHandleRequests<AddTaskCommand>, AddTaskCommandHandler>().AsMultiInstance();
            container.Register<ITaskListRetriever, TaskListRetriever>().AsMultiInstance();
            container.Register<ITasksDAO, TasksDAO>().AsMultiInstance();
            var logger = LogManager.GetLogger("TaskList");
            container.Register<ILog, ILog>(logger);
            container.Register<IAmARequestContextFactory, InMemoryRequestContextFactory>().AsMultiInstance();
            container.Register<IAmAMessageStore<Message>, RavenMessageStore>().AsSingleton();
            container.Register<IAmAMessagingGateway, RMQMessagingGateway>().AsSingleton();

            var handlerFactory = new TinyIocHandlerFactory(container);
            var messageMapperFactory = new TinyIoCMessageMapperFactory(container);

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

            //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
            var messageMapperRegistry = new MessageMapperRegistry(messageMapperFactory);

            //create the gateway
            var gateway = new RMQMessagingGateway(logger);

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

            resolver.AddDependencyInstance<IAmACommandProcessor>(commandProcessor, DependencyLifetime.Singleton);
            resolver.AddDependency<ITaskRetriever, TaskRetriever>(DependencyLifetime.Singleton);
            resolver.AddDependency<ITaskListRetriever, TaskListRetriever>(DependencyLifetime.Singleton);


            return PipelineContinuation.Continue;
        }
Ejemplo n.º 3
0
 public RMQInputChannelfactory(RMQMessagingGateway gateway)
 {
     this.gateway = gateway;
 }