Exemple #1
0
        private void RegisterCommandProcessor()
        {
            //create handler
            var servicesHandlerFactory = new ServicesHandlerFactoryAsync(_container);
            var subscriberRegistry     = new SubscriberRegistry();

            _container.Register <IHandleRequestsAsync <AddGreetingCommand>, AddGreetingCommandHandlerAsync>(Lifestyle.Scoped);

            subscriberRegistry.RegisterAsync <AddGreetingCommand, AddGreetingCommandHandlerAsync>();

            //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 retryPolicyAsync          = Policy.Handle <Exception>().WaitAndRetryAsync(new[] { TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150) });
            var circuitBreakerPolicyAsync = Policy.Handle <Exception>().CircuitBreakerAsync(1, TimeSpan.FromMilliseconds(500));
            var policyRegistry            = new PolicyRegistry()
            {
                { CommandProcessor.RETRYPOLICY, retryPolicy },
                { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy },
                { CommandProcessor.RETRYPOLICYASYNC, retryPolicyAsync },
                { CommandProcessor.CIRCUITBREAKERASYNC, circuitBreakerPolicyAsync }
            };


            var commandProcessor = CommandProcessorBuilder.With()
                                   .Handlers(new Paramore.Brighter.HandlerConfiguration(subscriberRegistry, servicesHandlerFactory))
                                   .Policies(policyRegistry)
                                   .NoTaskQueues()
                                   .RequestContextFactory(new Paramore.Brighter.InMemoryRequestContextFactory())
                                   .Build();

            _container.RegisterSingleton <IAmACommandProcessor>(commandProcessor);
        }
        private void RegisterCommandProcessor()
        {
            //create handler
            var subscriberRegistry = new SubscriberRegistry();

            RegisterBrighterHandlersFromAssembly(
                typeof(IHandleRequestsAsync <>),
                new Assembly[] { typeof(NewShipRegistrationHandlerAsync).Assembly },
                typeof(IHandleRequestsAsync <>).GetTypeInfo().Assembly,
                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 retryPolicyAsync          = Policy.Handle <Exception>().WaitAndRetryAsync(new[] { TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150) });
            var circuitBreakerPolicyAsync = Policy.Handle <Exception>().CircuitBreakerAsync(1, TimeSpan.FromMilliseconds(500));
            var policyRegistry            = new Paramore.Brighter.PolicyRegistry()
            {
                { CommandProcessor.RETRYPOLICY, retryPolicy },
                { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy },
                { CommandProcessor.RETRYPOLICYASYNC, retryPolicyAsync },
                { CommandProcessor.CIRCUITBREAKERASYNC, circuitBreakerPolicyAsync }
            };

            var servicesHandlerFactory = new ServicesHandlerFactoryAsync(_container);

            var messagingGatewayConfiguration = RmqGatewayBuilder.With.Uri(new Uri(Configuration["Broker:Uri"]))
                                                .Exchange(Configuration["Broker:Exchange"])
                                                .DefaultQueues();

            var gateway         = new RmqMessageProducer(messagingGatewayConfiguration);
            var sqlMessageStore = new MySqlMessageStore(new MySqlMessageStoreConfiguration(Configuration["Database:MessageStore"], Configuration["Database:MessageTableName"]));

            var messageMapperFactory = new MessageMapperFactory(_container);

            var messageMapperRegistry = new MessageMapperRegistry(messageMapperFactory);

            RegisterMessageMappersFromAssembly(
                new Assembly[] { typeof(LineNameUpdatedEventMessageMapper).Assembly },
                typeof(IAmAMessageMapper <>).GetTypeInfo().Assembly,
                messageMapperRegistry);

            var messagingConfiguration = new MessagingConfiguration(
                messageStore: sqlMessageStore,
                messageProducer: gateway,
                messageMapperRegistry: messageMapperRegistry);

            var commandProcessor = CommandProcessorBuilder.With()
                                   .Handlers(new Paramore.Brighter.HandlerConfiguration(subscriberRegistry, servicesHandlerFactory))
                                   .Policies(policyRegistry)
                                   .TaskQueues(messagingConfiguration)
                                   .RequestContextFactory(new Paramore.Brighter.InMemoryRequestContextFactory())
                                   .Build();

            _container.RegisterInstance <IAmACommandProcessor>(commandProcessor);
        }
        internal bool Retry(Action send)
        {
            var policy = PolicyRegistry.Get <Policy>(CommandProcessor.RETRYPOLICY);
            var result = policy.ExecuteAndCapture(send);

            if (result.Outcome != OutcomeType.Successful)
            {
                if (result.FinalException != null)
                {
                    s_logger.LogError(result.FinalException, "Exception whilst trying to publish message");
                    CheckOutstandingMessages();
                }

                return(false);
            }

            return(true);
        }
        private async Task <bool> RetryAsync(Func <CancellationToken, Task> send, bool continueOnCapturedContext = false,
                                             CancellationToken cancellationToken = default(CancellationToken))
        {
            var result = await PolicyRegistry.Get <AsyncPolicy>(CommandProcessor.RETRYPOLICYASYNC)
                         .ExecuteAndCaptureAsync(send, cancellationToken, continueOnCapturedContext)
                         .ConfigureAwait(continueOnCapturedContext);

            if (result.Outcome != OutcomeType.Successful)
            {
                if (result.FinalException != null)
                {
                    s_logger.LogError(result.FinalException, "Exception whilst trying to publish message");
                    CheckOutstandingMessages();
                }

                return(false);
            }

            return(true);
        }
Exemple #5
0
        private void RegisterCommandProcessor()
        {
            //create handler
            var subscriberRegistry = new SubscriberRegistry();

            _container.Register <IHandleRequestsAsync <AddToDoCommand>, AddToDoCommandHandlerAsync>(Lifestyle.Scoped);
            _container.Register <IHandleRequestsAsync <DeleteAllToDosCommand>, DeleteAllToDosCommandHandlerAsync>(Lifestyle.Scoped);
            _container.Register <IHandleRequestsAsync <DeleteToDoByIdCommand>, DeleteToDoByIdCommandHandlerAsync>(Lifestyle.Scoped);
            _container.Register <IHandleRequestsAsync <UpdateToDoCommand>, UpdateToDoCommandHandlerAsync>(Lifestyle.Scoped);

            subscriberRegistry.RegisterAsync <AddToDoCommand, AddToDoCommandHandlerAsync>();
            subscriberRegistry.RegisterAsync <DeleteAllToDosCommand, DeleteAllToDosCommandHandlerAsync>();
            subscriberRegistry.RegisterAsync <DeleteToDoByIdCommand, DeleteToDoByIdCommandHandlerAsync>();
            subscriberRegistry.RegisterAsync <UpdateToDoCommand, UpdateToDoCommandHandlerAsync>();

            //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 retryPolicyAsync          = Policy.Handle <Exception>().WaitAndRetryAsync(new[] { TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150) });
            var circuitBreakerPolicyAsync = Policy.Handle <Exception>().CircuitBreakerAsync(1, TimeSpan.FromMilliseconds(500));
            var policyRegistry            = new PolicyRegistry()
            {
                { CommandProcessor.RETRYPOLICY, retryPolicy },
                { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy },
                { CommandProcessor.RETRYPOLICYASYNC, retryPolicyAsync },
                { CommandProcessor.CIRCUITBREAKERASYNC, circuitBreakerPolicyAsync }
            };

            var servicesHandlerFactory = new ServicesHandlerFactoryAsync(_container);

            var messagingGatewayConfiguration = RmqGatewayBuilder.With.Uri(new Uri(Configuration["RabbitMQ:Uri"])).Exchange(Configuration["RabbitMQ:Exchange"]).DefaultQueues();

            var gateway         = new RmqMessageProducer(messagingGatewayConfiguration);
            var sqlMessageStore = new MySqlMessageStore(new MySqlMessageStoreConfiguration(Configuration["Database:MessageStore"], Configuration["Database:MessageTableName"]));

            var messageMapperFactory = new MessageMapperFactory(_container);

            _container.Register <IAmAMessageMapper <BulkAddToDoCommand>, BulkAddToDoMessageMapper>();
            _container.Register <IAmAMessageMapper <TaskCompletedEvent>, TaskCompleteEventMessageMapper>();
            _container.Register <IAmAMessageMapper <TaskCreatedEvent>, TaskCreatedEventMessageMapper>();

            var messageMapperRegistry = new MessageMapperRegistry(messageMapperFactory)
            {
                { typeof(BulkAddToDoCommand), typeof(BulkAddToDoMessageMapper) },
                { typeof(TaskCompletedEvent), typeof(TaskCompleteEventMessageMapper) },
                { typeof(TaskCreatedEvent), typeof(TaskCreatedEventMessageMapper) }
            };

            var messagingConfiguration = new MessagingConfiguration(
                messageStore: sqlMessageStore,
                messageProducer: gateway,
                messageMapperRegistry: messageMapperRegistry);

            var commandProcessor = CommandProcessorBuilder.With()
                                   .Handlers(new Paramore.Brighter.HandlerConfiguration(subscriberRegistry, servicesHandlerFactory))
                                   .Policies(policyRegistry)
                                   .TaskQueues(messagingConfiguration)
                                   .RequestContextFactory(new Paramore.Brighter.InMemoryRequestContextFactory())
                                   .Build();

            _container.RegisterSingleton <IAmACommandProcessor>(commandProcessor);
        }