Ozkan
Ozkan

Reputation: 4170

Disable round-robin message consumption on MassTransit

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();

Upvotes: 5

Views: 1755

Answers (1)

Ozkan
Ozkan

Reputation: 4170

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();

Upvotes: 6

Related Questions