示例#1
0
        static void Main(string[] args)
        {
            DataBus bus = null;

            DependencyResolver.SetDependencyResolver(new NinjectBusDependencyResolver());
            try
            {
                EndpointConvention.Map <TestMessage>(new Uri("rabbitmq://localhost/testmessage_queue"));
                MessageCorrelation.UseCorrelationId <TestMessage>(x => x.CorrelationId);
                bus = new DataBus("domer");
                bus.Start();

                for (int i = 0; i < MessageCount; i++)
                {
                    bus.Publish <TestMessage>(new TestMessage()
                    {
                        Message = $"Hello World{i}"
                    }).Wait();
                }

                //Thread.Sleep(TimeSpan.FromSeconds(20));
                Console.WriteLine($"Количество обработанных сообщений: {TestMessageHandler.Counter}");
                Console.WriteLine($"Время затрачено: {TestMessageHandler.WorkTime}");
                Console.ReadLine();
            }
            finally
            {
                bus?.Stop();
            }
        }
示例#2
0
文件: Startup.cs 项目: thangozil/crm
        public static IServiceCollection AddMassTransit(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddMassTransit((provider) =>
            {
                MessageCorrelation.UseCorrelationId <IMessage>(x => x.CorrelationId);
                var rabbitMqOption = configuration.GetOptions <RabbitMqOptions>("rabbitMQ");

                return(Bus.Factory.CreateUsingRabbitMq(cfg =>
                {
                    cfg.UseSerilog();
                    var host = cfg.Host(new Uri(rabbitMqOption.Url), "/", hc =>
                    {
                        hc.Username(rabbitMqOption.UserName);
                        hc.Password(rabbitMqOption.Password);
                    });

                    cfg.ReceiveEndpoint("communication", x =>
                    {
                        x.Consumer <ContactCreatedConsumer>(provider);
                        x.Consumer <ContactUpdatedConsumer>(provider);
                    });

                    cfg.PropagateOpenTracingContext();
                    cfg.PropagateCorrelationIdContext();
                }));
            }, (cfg) =>
            {
                cfg.AddConsumersFromNamespaceContaining <ConsumerAnchor>();
            });

            return(services);
        }
示例#3
0
        public static void Main()
        {
            // Use the OrderId as the message CorrelationId
            GlobalTopology.Send.UseCorrelationId <SubmitOrder>(x => x.OrderId);

            // Previous approach, which now calls the new way above
            MessageCorrelation.UseCorrelationId <SubmitOrder>(x => x.OrderId);
        }
        protected override void ConfigureServiceBusBusHost(IServiceBusBusFactoryConfigurator configurator, IServiceBusHost host)
        {
            MessageCorrelation.UseCorrelationId <LegacyMessage>(x => x.TransactionId);

            configurator.Send <IEvent>(x =>
            {
                x.UseCorrelationId(p => p.TransactionId);
            });
        }
示例#5
0
        protected override void ConfigureRabbitMqBus(IRabbitMqBusFactoryConfigurator configurator)
        {
            MessageCorrelation.UseCorrelationId <LegacyMessage>(x => x.TransactionId);

            configurator.Send <IEvent>(x =>
            {
                x.UseCorrelationId(p => p.TransactionId);
            });
        }
        public async Task Should_have_a_familiar_syntax()
        {
            MessageCorrelation.UseCorrelationId<LegacyMessage>(x => x.TransactionId);

            var harness = new InMemoryTestHarness();
            harness.OnConfigureInMemoryBus += configurator =>
            {
                configurator.Send<IEvent>(x =>
                {
                    x.UseCorrelationId(p => p.TransactionId);
                });
            };

            Task<ConsumeContext<INewUserEvent>> handled = null;
            Task<ConsumeContext<OtherMessage>> otherHandled = null;
            Task<ConsumeContext<LegacyMessage>> legacyHandled = null;
            harness.OnConfigureInMemoryReceiveEndpoint += configurator =>
            {
                handled = harness.Handled<INewUserEvent>(configurator);
                otherHandled = harness.Handled<OtherMessage>(configurator);
                legacyHandled = harness.Handled<LegacyMessage>(configurator);
            };


            await harness.Start();
            try
            {
                var transactionId = NewId.NextGuid();

                await harness.InputQueueSendEndpoint.Send<INewUserEvent>(new {TransactionId = transactionId});

                ConsumeContext<INewUserEvent> context = await handled;

                Assert.IsTrue(context.CorrelationId.HasValue);
                Assert.That(context.CorrelationId.Value, Is.EqualTo(transactionId));

                await harness.InputQueueSendEndpoint.Send<OtherMessage>(new {CorrelationId = transactionId});

                ConsumeContext<OtherMessage> otherContext = await otherHandled;

                Assert.IsTrue(otherContext.CorrelationId.HasValue);
                Assert.That(otherContext.CorrelationId.Value, Is.EqualTo(transactionId));

                await harness.InputQueueSendEndpoint.Send<LegacyMessage>(new {TransactionId = transactionId});

                ConsumeContext<LegacyMessage> legacyContext = await legacyHandled;

                Assert.IsTrue(legacyContext.CorrelationId.HasValue);
                Assert.That(legacyContext.CorrelationId.Value, Is.EqualTo(transactionId));
            }
            finally
            {
                await harness.Stop();
            }
        }
示例#7
0
 static RoutingSlipEventPublisher()
 {
     MessageCorrelation.UseCorrelationId <RoutingSlipCompleted>(x => x.TrackingNumber);
     MessageCorrelation.UseCorrelationId <RoutingSlipFaulted>(x => x.TrackingNumber);
     MessageCorrelation.UseCorrelationId <RoutingSlipActivityCompleted>(x => x.ExecutionId);
     MessageCorrelation.UseCorrelationId <RoutingSlipActivityFaulted>(x => x.ExecutionId);
     MessageCorrelation.UseCorrelationId <RoutingSlipActivityCompensated>(x => x.ExecutionId);
     MessageCorrelation.UseCorrelationId <RoutingSlipActivityCompensationFailed>(x => x.ExecutionId);
     MessageCorrelation.UseCorrelationId <RoutingSlipCompensationFailed>(x => x.TrackingNumber);
     MessageCorrelation.UseCorrelationId <RoutingSlipTerminated>(x => x.TrackingNumber);
     MessageCorrelation.UseCorrelationId <RoutingSlipRevised>(x => x.TrackingNumber);
 }
 public static void Setup()
 {
     MessageCorrelation.UseCorrelationId <CreateEntityResponse>(x => x.EntityId);
     MessageCorrelation.UseCorrelationId <UpdateEntityMetadata>(x => x.EntityId);
     MessageCorrelation.UseCorrelationId <GetEntityMetadataRequest>(x => x.EntityId);
     MessageCorrelation.UseCorrelationId <GetEntityMetadataResponse>(x => x.EntityId);
     MessageCorrelation.UseCorrelationId <GetEntityTransformRequest>(x => x.EntityId);
     MessageCorrelation.UseCorrelationId <GetEntityTransformResponse>(x => x.EntityId);
     MessageCorrelation.UseCorrelationId <UpdateTransform>(x => x.EntityId);
     EndpointConvention.Map <UpdateEntityMetadata>(new Uri("queue:update-entity-metadata"));
     EndpointConvention.Map <UpdateTransform>(new Uri("queue:update-transform"));
 }
示例#9
0
        public static IBusControl ConfigureBusForAzure(Action <IServiceBusBusFactoryConfigurator, IServiceBusHost> registrationAction = null)
        {
            return(Bus.Factory.CreateUsingAzureServiceBus(cfg =>
            {
                var host = cfg.Host(AzureServiceBusConfiguration.AzureConnectionString, hst =>
                {
                    hst.OperationTimeout = TimeSpan.FromSeconds(15);
                });

                MessageCorrelation.UseCorrelationId <IRegisterOrder>(x => x.Id);

                registrationAction?.Invoke(cfg, host);
            }));
        }
示例#10
0
        public static IBusControl ConfigureBusForRabbitMQ(Action <IRabbitMqBusFactoryConfigurator, IRabbitMqHost> registrationAction = null)
        {
            return(Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
                var host = cfg.Host(new Uri(RabbitMqConfiguration.RabbitMqUri), hst =>
                {
                    hst.Username(RabbitMqConfiguration.UserName);
                    hst.Password(RabbitMqConfiguration.Password);
                });

                MessageCorrelation.UseCorrelationId <IRegisterOrder>(x => x.Id);

                registrationAction?.Invoke(cfg, host);
            }));
        }
示例#11
0
        public static void ConfigureMasstransit(this ContainerBuilder builder)
        {
            builder.AddMassTransit(configurator =>
            {
                //Add all consumers in the current loaded domain to masstransit.
                //Definition: Every class that implements the IConsumer<,>
                var assemblies = AppDomain.CurrentDomain.GetAssemblies();
                configurator.AddConsumers(assemblies);

                configurator.AddBus(ctx => { return(Bus.Factory.CreateUsingRabbitMq(rabbit =>
                    {
                        rabbit.Host("rabbitmq://localhost");
                        rabbit.ReceiveEndpoint("my-application", endpoint =>
                        {
                            //configure all consumers that Autofac added.
                            endpoint.ConfigureConsumers(ctx);

                            //Prefetch count = Un-acked messages sent from the broker to MassTransit.
                            //Concurrency limit = Acts as a semaphore to limit the numbers of concurrent tasks to run of this consumer
                            //For systems that is supposed to process alot of data, these values should be set to a "high value".
                            //There is not recommended values for these two, so you just have to play around with them.
                            endpoint.PrefetchCount = 16;
                            endpoint.UseConcurrencyLimit(10);

                            //If an exception occurs inside a consumer, we can configure the retry strategy.
                            endpoint.UseRetry(retry =>
                            {
                                //retry.Exponential()
                                //retry.Immediate()
                                //retry.Interval()
                                retry.Incremental(2, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));

                                //If a consumer throws an ArgumentException, ignore this and do not retry it.
                                retry.Ignore <ArgumentException>();
                            });
                        });
                    })); });
            });

            MessageCorrelation.UseCorrelationId <CorrelatedEvent>(x => x.CorrelationId = Guid.NewGuid());
        }
示例#12
0
        public static void ConfigureMessageCorrelation()
        {
            lock (_lock)
            {
                if (_configured)
                {
                    return;
                }
                MessageCorrelation.UseCorrelationId <RoutingSlipCompleted>(x => x.TrackingNumber);
                MessageCorrelation.UseCorrelationId <RoutingSlipFaulted>(x => x.TrackingNumber);
                MessageCorrelation.UseCorrelationId <RoutingSlipActivityCompleted>(x => x.ExecutionId);
                MessageCorrelation.UseCorrelationId <RoutingSlipActivityFaulted>(x => x.ExecutionId);
                MessageCorrelation.UseCorrelationId <RoutingSlipActivityCompensated>(x => x.ExecutionId);
                MessageCorrelation.UseCorrelationId <RoutingSlipActivityCompensationFailed>(x => x.ExecutionId);
                MessageCorrelation.UseCorrelationId <RoutingSlipCompensationFailed>(x => x.TrackingNumber);
                MessageCorrelation.UseCorrelationId <RoutingSlipTerminated>(x => x.TrackingNumber);
                MessageCorrelation.UseCorrelationId <RoutingSlipRevised>(x => x.TrackingNumber);

                _configured = true;
            }
        }
示例#13
0
        private IBusControl ConfigureBus(IServiceProvider provider)
        {
            MessageCorrelation.UseCorrelationId <IMessage>(x => x.CorrelationId);

            return(Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
                cfg.UseSerilog();
                var host = cfg.Host(new Uri(RabbitMqOption.Url), "/", hc =>
                {
                    hc.Username(RabbitMqOption.UserName);
                    hc.Password(RabbitMqOption.Password);
                });

                cfg.ReceiveEndpoint(host, "contact", x =>
                {
                    x.ConfigureConsumer <ContactCreatedConsumer>(provider);
                });

                cfg.PropagateOpenTracingContext();
                cfg.PropagateCorrelationIdContext();
            }));
        }
示例#14
0
文件: Startup.cs 项目: nongtran/crm
        private static IBusControl ConfigureBus(IServiceProvider provider)
        {
            MessageCorrelation.UseCorrelationId <IMessage>(x => x.CorrelationId);

            return(Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
                cfg.UseSerilog();
                var rabbitmqOption = provider.GetService <IConfiguration>().GetOptions <RabbitMqOptions>("rabbitMQ");
                var host = cfg.Host(new Uri(rabbitmqOption.Url), "/", hc =>
                {
                    hc.Username(rabbitmqOption.UserName);
                    hc.Password(rabbitmqOption.Password);
                });

                cfg.ReceiveEndpoint(host, "contact", x =>
                {
                    x.Consumer <ContactCreatedConsumer>(provider);
                });

                cfg.ConfigureEndpoints(provider, new KebabCaseEndpointNameFormatter());
                cfg.PropagateCorrelationIdContext();
                cfg.PropagateOpenTracingContext();
            }));
        }
        private static IBusControl ConfigureBus()
        {
            var bus = MassTransit.Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
                cfg.Host(new Uri("rabbitmq://localhost"), h =>
                {
                    h.Username("guest");
                    h.Password("guest");
                });

                // request the log to Log4Net
                cfg.UseLog4Net();

                // tel MT to use a specific message property as correlationId
                MessageCorrelation.UseCorrelationId <IOrderSubmittedWithoutCorrelatedBy>(x => x.SomeGuidValue);

                cfg.ReceiveEndpoint("submit_order_correlated_queue", e =>
                {
                    e.Consumer <SubmitOrderCorrelatedConsumer>();
                });

                cfg.ReceiveEndpoint("ship_order_correlated_queue", e =>
                {
                    e.Consumer <ShipOrderCorrelatedConsumer>();
                });

                cfg.ReceiveEndpoint("bill_order_correlated_queue", e =>
                {
                    e.Consumer <BillOrderCorrelatedConsumer>();
                });
            });

            _sendEndpointTask = bus.GetSendEndpoint(new Uri("rabbitmq://localhost/submit_order_correlated_queue"));

            return(bus);
        }
示例#16
0
 static RoutingSlipExtensions()
 {
     MessageCorrelation.UseCorrelationId <RoutingSlip>(x => x.TrackingNumber);
 }