C# – How to configure multiple message consumers in MassTransit

cmasstransitpublish-subscribeservicebus

I'm looking at using MassTransit to publish a event from one process to all other processes that care about that event. In my environment, several processes that are subscribed to that event could be running on the same box.

Based on the MassTransit's example code, here's a VERY simple proof-of-concept that does not attempt to deal with multi-cast (processes will also be distributed across many machines). Consider the "Message" below as an "Event", and that I want every instance of this process to get that message:

using System;
using MassTransit;

namespace BasicPubSub
{
    public class Message
    {
        public String Text { get; set; }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            //initialize the bus
            var bus = ServiceBusFactory.New(sbc =>
            {
                sbc.UseMsmq();

                //cause the DTC and MSMQ to get installed/fixed if needed.
                sbc.VerifyMsDtcConfiguration();
                sbc.VerifyMsmqConfiguration();

                //sbc.UseMulticastSubscriptionClient();//Doesn't behave for a private queue.
                sbc.ReceiveFrom("msmq://localhost/test_queue");

                //Listen for interesting events.
                sbc.Subscribe(subs =>
                {
                    subs.Handler<Message>(msg => Console.WriteLine(msg.Text));
                });
            });

            //stay open until the user types "exit"
            var message = "";
            do
            {
                message = Console.ReadLine();
                //broadcast the message
                bus.Publish(new Message { Text = message });
            } while (message != "exit");
        }
    }
}

If I run multiple instances of this C# app, only one of the instances receives the message, but I need all of the instances to receive it. This behavior is consistent with the classical notion of a queue, so I'm fine with that.

What I am looking to know is whether I can use MSMQ/MassTransit to publish a message that all subscribers will receive, as opposed to just a single subscriber dequeuing it and the others not receiving it. Perhaps I'm trying to use a hammer and I need a paintbrush?

Just to be clear here, I am not looking at sharing a single queue for different kinds of messages, I'm looking to setup "peer-to-peer" pub-sub for a specific type of message, using MT and MSMQ.

Best Answer

I'm not sure about hammers or paintbrushes, but I think you're heading in the right direction.

To start with, each instance of a bus requires it's own queue to read from. If you are running multiple versions of this same program, make sure they are reading off different queues. Attempting to read from the same queue leads to Bad Stuff(TM).

Join the mailing list if you have more questions. https://groups.google.com/forum/#!forum/masstransit-discuss.

When publishing, all consumers should recieve the message once they have been subscribed. It might take a second or two for the subscription data to pass to all processes when starting up.

I would also suggest taking a look at RabbitMQ. Unless you absolutely need DTC, RabbitMQ is a much more robust messaging platform.

Related Topic