Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 658
Ozkan Avatar asked Sep 19 '16 12:09

Ozkan


1 Answers

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();
like image 118
Ozkan Avatar answered Sep 27 '22 20:09

Ozkan