I have created a basic demo pub/sub application which works on localhost with MassTransit.
What I want to achieve is to publish a message and all the subscribers should receive the message.
At the moment, in my environment I start one publisher app and two subscriber apps. But when I publish a message the subscribers receive the message in turns.
My pub/sub code:
Publish:
var bus = Bus.Factory.CreateUsingRabbitMq(config =>
{
config.Host(new Uri("rabbitmq://localhost/"), h => { });
config.ExchangeType = ExchangeType.Fanout;
});
var busHandle = bus.Start();
bus.Publish<SomethingHappened>(message);
Subscribers use this code:
var bus = Bus.Factory.CreateUsingRabbitMq(config =>
{
var host = config.Host(new Uri("rabbitmq://localhost/"), h => { });
config.ReceiveEndpoint(host, "MassTransitExample_Queue", e => e.Consumer<SomethingHappenedConsumer>());
});
var busHandle = bus.Start();
Console.ReadKey();
busHandle.Stop();
When reading the article below I found that the queue name must be unique
https://www.maldworth.com/2015/10/27/masstransit-send-vs-publish/
When building your bus and registering an endpoint like so: sbc.ReceiveEndpoint(...), one has to be sure that the queueName parameter is unique.
So my subscribers code looks like this now:
var bus = Bus.Factory.CreateUsingRabbitMq(config =>
{
var host = config.Host(new Uri("rabbitmq://localhost/"), h => { });
config.ReceiveEndpoint(host, "MTExQueue_" + Guid.NewGuid().ToString(), e => e.Consumer<SomethingHappenedConsumer>());
});
var busHandle = bus.Start();
Console.ReadKey();
busHandle.Stop();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With