Exemplo n.º 1
0
        public static async Task Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .Enrich.FromLogContext()
                         .WriteTo.Console()
                         .CreateLogger();

            var host = new HostBuilder()
                       .ConfigureServices((hostContext, services) =>

            {
                var connections = new Connection[]
                {
                    new Connection <GreetingEvent>(
                        new ConnectionName("paramore.example.greeting"),
                        new ChannelName(typeof(GreetingEvent).FullName.ToValidSNSTopicName()),
                        new RoutingKey(typeof(GreetingEvent).FullName.ToValidSNSTopicName()),
                        timeoutInMilliseconds: 200,
                        isDurable: true,
                        highAvailability: true)
                };

                //create the gateway
                if (new CredentialProfileStoreChain().TryGetAWSCredentials("default", out var credentials))
                {
                    var awsConnection = new AWSMessagingGatewayConnection(credentials, RegionEndpoint.EUWest1);

                    var sqsMessageConsumerFactory = new SqsMessageConsumerFactory(awsConnection);

                    services.AddServiceActivator(options =>
                    {
                        options.Connections       = connections;
                        options.ChannelFactory    = new ChannelFactory(awsConnection, sqsMessageConsumerFactory);
                        var outBox                = new InMemoryOutbox();
                        options.BrighterMessaging = new BrighterMessaging(outBox, outBox, new SqsMessageProducer(awsConnection), null);
                    }).AutoFromAssemblies();
                }

                services.AddHostedService <ServiceActivatorHostedService>();
            })
                       .UseConsoleLifetime()
                       .UseSerilog()
                       .Build();

            await host.RunAsync();
        }
Exemplo n.º 2
0
        public DocumentService()
        {
            log4net.Config.XmlConfigurator.Configure();


            var container = new TinyIoCContainer();

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

            container.Register <IHandleRequests <DocumentCreatedEvent>, DocumentCreatedEventHandler>();
            container.Register <IHandleRequests <DocumentUpdatedEvent>, DocumentUpdatedEventHandler>();
            container.Register <IHandleRequests <FolderCreatedEvent>, FolderCreatedEventHandler>();

            var subscriberRegistry = new SubscriberRegistry();

            subscriberRegistry.Register <DocumentCreatedEvent, DocumentCreatedEventHandler>();
            subscriberRegistry.Register <DocumentUpdatedEvent, DocumentUpdatedEventHandler>();
            subscriberRegistry.Register <FolderCreatedEvent, FolderCreatedEventHandler>();

            //create policies
            var retryPolicy = Policy
                              .Handle <Exception>()
                              .WaitAndRetry(new[]
            {
                TimeSpan.FromMilliseconds(5000),
                TimeSpan.FromMilliseconds(10000),
                TimeSpan.FromMilliseconds(10000)
            });

            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)
            {
                { typeof(FolderCreatedEvent), typeof(FolderCreatedEventMessageMapper) },
                { typeof(DocumentCreatedEvent), typeof(DocumentCreatedEventMessageMapper) },
                { typeof(DocumentUpdatedEvent), typeof(DocumentUpdatedEventMessageMapper) }
            };

            var sqsMessageConsumerFactory = new SqsMessageConsumerFactory();
            var sqsMessageProducerFactory = new SqsMessageProducerFactory();

            var builder = DispatchBuilder
                          .With()
                          .CommandProcessor(CommandProcessorBuilder.With()
                                            .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory))
                                            .Policies(policyRegistry)
                                            .NoTaskQueues()
                                            .RequestContextFactory(new InMemoryRequestContextFactory())
                                            .Build()
                                            )
                          .MessageMappers(messageMapperRegistry)
                          .ChannelFactory(new InputChannelFactory(sqsMessageConsumerFactory, sqsMessageProducerFactory))
                          .ConnectionsFromConfiguration();

            _dispatcher = builder.Build();
        }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InputChannelFactory"/> class.
 /// </summary>
 /// <param name="messageConsumerFactory">The messageConsumerFactory.</param>
 /// <param name="messageProducerFactory">The messageProducerFactory.</param>
 public InputChannelFactory(SqsMessageConsumerFactory messageConsumerFactory, SqsMessageProducerFactory messageProducerFactory)
 {
     _messageConsumerFactory = messageConsumerFactory;
     _messageProducerFactory = messageProducerFactory;
 }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InputChannelFactory"/> class.
 /// </summary>
 /// <param name="messageConsumerFactory">The messageConsumerFactory.</param>
 /// <param name="messageProducerFactory">The messageProducerFactory.</param>
 public InputChannelFactory(SqsMessageConsumerFactory messageConsumerFactory, SqsMessageProducerFactory messageProducerFactory)
 {
     _messageConsumerFactory = messageConsumerFactory;
     _messageProducerFactory = messageProducerFactory;
 }
Exemplo n.º 5
0
        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .WriteTo.LiterateConsole()
                         .CreateLogger();

            var container = new TinyIoCContainer();

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

            container.Register <IHandleRequests <GreetingEvent>, GreetingEventHandler>();

            var subscriberRegistry = new SubscriberRegistry();

            subscriberRegistry.Register <GreetingEvent, GreetingEventHandler>();

            //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)
            {
                { typeof(GreetingEvent), typeof(GreetingEventMessageMapper) }
            };

            //create the gateway
            if (new CredentialProfileStoreChain().TryGetAWSCredentials("default", out var credentials))
            {
                var awsConnection = new AWSMessagingGatewayConnection(credentials, RegionEndpoint.EUWest1);

                var sqsMessageConsumerFactory = new SqsMessageConsumerFactory(awsConnection);

                var dispatcher = DispatchBuilder.With()
                                 .CommandProcessor(CommandProcessorBuilder.With()
                                                   .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory))
                                                   .Policies(policyRegistry)
                                                   .NoTaskQueues()
                                                   .RequestContextFactory(new InMemoryRequestContextFactory())
                                                   .Build())
                                 .MessageMappers(messageMapperRegistry)
                                 .DefaultChannelFactory(new ChannelFactory(awsConnection, sqsMessageConsumerFactory))
                                 .Connections(new Connection[]
                {
                    new Connection <GreetingEvent>(
                        new ConnectionName("paramore.example.greeting"),
                        new ChannelName(typeof(GreetingEvent).FullName.ToValidSNSTopicName()),
                        new RoutingKey(typeof(GreetingEvent).FullName.ToValidSNSTopicName()),
                        timeoutInMilliseconds: 200,
                        isDurable: true,
                        highAvailability: true)
                }).Build();

                dispatcher.Receive();

                Console.WriteLine("Press Enter to stop ...");
                Console.ReadLine();

                dispatcher.End().Wait();
            }
        }
Exemplo n.º 6
0
        public DocumentService()
        {
            log4net.Config.XmlConfigurator.Configure();


            var container = new TinyIoCContainer();

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

            container.Register <IHandleRequests <DocumentCreatedEvent>, DocumentCreatedEventHandler>();
            container.Register <IHandleRequests <DocumentUpdatedEvent>, DocumentUpdatedEventHandler>();
            container.Register <IHandleRequests <FolderCreatedEvent>, FolderCreatedEventHandler>();

            var subscriberRegistry = new SubscriberRegistry();

            subscriberRegistry.Register <DocumentCreatedEvent, DocumentCreatedEventHandler>();
            subscriberRegistry.Register <DocumentUpdatedEvent, DocumentUpdatedEventHandler>();
            subscriberRegistry.Register <FolderCreatedEvent, FolderCreatedEventHandler>();

            //create policies
            var retryPolicy = Policy
                              .Handle <Exception>()
                              .WaitAndRetry(new[]
            {
                TimeSpan.FromMilliseconds(5000),
                TimeSpan.FromMilliseconds(10000),
                TimeSpan.FromMilliseconds(10000)
            });

            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)
            {
                { typeof(FolderCreatedEvent), typeof(FolderCreatedEventMessageMapper) },
                { typeof(DocumentCreatedEvent), typeof(DocumentCreatedEventMessageMapper) },
                { typeof(DocumentUpdatedEvent), typeof(DocumentUpdatedEventMessageMapper) }
            };

            var awsCredentials = new StoredProfileAWSCredentials();


            var sqsMessageConsumerFactory = new SqsMessageConsumerFactory(awsCredentials);
            var sqsMessageProducerFactory = new SqsMessageProducerFactory(awsCredentials);

            var connections = new List <Connection>
            {
                new Connection(
                    new ConnectionName("paramore.example.documentsandfolders.documentcreatedevent"),
                    new InputChannelFactory(sqsMessageConsumerFactory, sqsMessageProducerFactory),
                    typeof(DocumentCreatedEvent),
                    new ChannelName("https://sqs.eu-west-1.amazonaws.com/027649620536/DocumentCreatedEvent"),
                    "DocumentCreatedEvent",
                    timeoutInMilliseconds: 5000,
                    noOfPerformers: 10),
                new Connection(
                    new ConnectionName("paramore.example.documentsandfolders.documentupdatedevent"),
                    new InputChannelFactory(sqsMessageConsumerFactory, sqsMessageProducerFactory),
                    typeof(DocumentUpdatedEvent),
                    new ChannelName("https://sqs.eu-west-1.amazonaws.com/027649620536/DocumentUpdatedEvent"),
                    "DocumentUpdatedEvent",
                    timeoutInMilliseconds: 5000,
                    noOfPerformers: 10),
                new Connection(
                    new ConnectionName("paramore.example.documentsandfolders.foldercreateddevent"),
                    new InputChannelFactory(sqsMessageConsumerFactory, sqsMessageProducerFactory),
                    typeof(FolderCreatedEvent),
                    new ChannelName("https://sqs.eu-west-1.amazonaws.com/027649620536/FolderCreatedEvent"),
                    "FolderCreatedEvent",
                    timeoutInMilliseconds: 5000,
                    noOfPerformers: 10)
            };



            var builder = DispatchBuilder
                          .With()
                          .CommandProcessor(CommandProcessorBuilder.With()
                                            .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory))
                                            .Policies(policyRegistry)
                                            .NoTaskQueues()
                                            .RequestContextFactory(new InMemoryRequestContextFactory())
                                            .Build()
                                            )
                          .MessageMappers(messageMapperRegistry)
                          .ChannelFactory(new InputChannelFactory(sqsMessageConsumerFactory, sqsMessageProducerFactory))
                          .Connections(connections);

            _dispatcher = builder.Build();
        }