示例#1
0
        public void ListenForRegisterOrderCommand()
        {
            channel.QueueDeclare(
                queue: RabbitMqConstants.GetRegisterOrderQueue("Registration"),
                durable: false, exclusive: false,
                autoDelete: false, arguments: null);

            channel.BasicQos(prefetchSize: 0, prefetchCount: 1,
                             global: false);

            var consumer = new RegisteredOrderCommandConsumer(this);

            channel.BasicConsume(
                queue: RabbitMqConstants.GetRegisterOrderQueue("Registration"),
                noAck: false,
                consumer: consumer);
        }
示例#2
0
        public void SendOrderRegisteredEvent(IOrderRegistered command)
        {
            channel.ExchangeDeclare(
                exchange: RabbitMqConstants.GetOrderRegisteredExchange(),
                type: ExchangeType.Fanout);

            var serializedCommand = JsonConvert.SerializeObject(command);

            var messageProperties = channel.CreateBasicProperties();

            messageProperties.ContentType = RabbitMqConstants.JsonMimeType;

            channel.BasicPublish(
                exchange: RabbitMqConstants.GetOrderRegisteredExchange(),
                routingKey: "",
                basicProperties: messageProperties,
                body: Encoding.UTF8.GetBytes(serializedCommand));
        }
        public void ListenForOrderRegisteredEvent()
        {
            #region queue and qos setup

            channel.ExchangeDeclare(
                exchange: RabbitMqConstants.GetOrderRegisteredExchange(),
                type: ExchangeType.Fanout);

            channel.QueueDeclare(
                queue: RabbitMqConstants.GetOrderRegisteredQueue("Notification"),
                durable: false, exclusive: false,
                autoDelete: false, arguments: null);

            channel.QueueBind(
                queue: RabbitMqConstants.GetOrderRegisteredQueue("Notification"),
                exchange: RabbitMqConstants.GetOrderRegisteredExchange(),
                routingKey: "");

            channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
            #endregion
            var eventingConsumer = new EventingBasicConsumer(channel);
            eventingConsumer.Received += (chan, eventArgs) =>
            {
                var contentType = eventArgs.BasicProperties.ContentType;
                if (contentType != RabbitMqConstants.JsonMimeType)
                {
                    throw new ArgumentException(
                              $"Can't handle content type {contentType}");
                }

                var message       = Encoding.UTF8.GetString(eventArgs.Body);
                var orderConsumer = new OrderRegisteredConsumer();
                var commandObj    =
                    JsonConvert.DeserializeObject <OrderRegistered>(message);
                orderConsumer.Consume(commandObj);
                channel.BasicAck(deliveryTag: eventArgs.DeliveryTag,
                                 multiple: false);
            };
            channel.BasicConsume(
                queue: RabbitMqConstants.GetOrderRegisteredQueue("Notification"),
                noAck: false,
                consumer: eventingConsumer);
        }
        public void SendRegisterOrderCommand(IRegisterOrder command)
        {
            channel.ExchangeDeclare(
                exchange: RabbitMqConstants.GetRegisterOrderExchange(),
                type: ExchangeType.Direct);

            var serializedCommand = JsonConvert.SerializeObject(command);

            var messageProperties = channel.CreateBasicProperties();

            messageProperties.ContentType =
                RabbitMqConstants.JsonMimeType;

            channel.BasicPublish(
                exchange: "",
                routingKey: RabbitMqConstants.GetRegisterOrderQueue("Registration"),
                basicProperties: messageProperties,
                body: Encoding.UTF8.GetBytes(serializedCommand));
        }
示例#5
0
        private static void Main()
        {
            using (var container = new Container())
            {
                container.Options.AllowResolvingFuncFactories();
                // configuration appsettings convention
                IConfiguration config = new ConfigurationBuilder()
                                        .SetBasePath(Directory.GetCurrentDirectory())
                                        .AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true)
                                        .AddEnvironmentVariables()
                                        .Build();
                container.Options.RegisterParameterConvention(new AppSettingsConvention(key => config[key]));

                ILoggerFactory loggerFactory = new LoggerFactory()
                                               .AddConsole()
                                               .AddDebug();
                container.Options.DependencyInjectionBehavior = new MsContextualLoggerInjectionBehavior(loggerFactory, container);

                ILogger logger = loggerFactory.CreateLogger <Program>();
                //container.RegisterSingleton<ILogger>(logger);
                logger.LogInformation("Starting BC 'Ritten' host...");

                ExampleCQRS.Infrastructure.Registrations.InfrastructureModule.RegisterEventBus(container, config);

                DomainModule.RegisterAll(container);
                ApplicationModule.RegisterAll(container);
                InfrastructureModule.RegisterAll(container);
                InfrastructureModule.RegisterEventForwarder(container);
                RabbitMqModule.RegisterCommandConsumers(container);
                RabbitMqModule.RegisterEventConsumers(container);

                //ReadModel.Infrastructure.Registrations.InfrastructureModule.RegisterAll(container);

                container.RegisterSingleton(RabbitMqConfiguration.ConfigureBus((cfg, host) =>
                {
                    // command queue
                    //cfg.ReceiveEndpoint(host,
                    //    RabbitMqConstants.CommandsQueue, e =>
                    //    {
                    //        e.Handler<ICommand>(context =>
                    //        Console.Out.WriteLineAsync($"Command received : {context.Message.GetType()}"));
                    //        //e.LoadFrom(container);// TODO: prevent receiving same events
                    //    });
                    // events queue
                    cfg.ReceiveEndpoint(host, RabbitMqConstants.GetEventsQueue(BoundedContextName), e =>
                    {
                        e.Handler <IDomainEvent>(context =>
                                                 Console.Out.WriteLineAsync($"Event received : {context.Message.GetType()}"));
                        e.LoadFrom(container);
                    });
                }));

                EventMappings.Configure();

                var eventBus = container.GetInstance <IIntegrationEventBus>();
                eventBus.Subscribe <KmStandCreated, RitService>();

                //var bus = container.GetInstance<IBusControl>();

                //bus.StartAsync();

                Console.WriteLine("Listening for commands.. Press enter to exit");
                Console.ReadLine();

                //bus.StopAsync();
            }
        }