7

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.

Andrew Theken
  • 3,392
  • 1
  • 31
  • 54
  • any solution on this. I am developing a app that requires msg send to all and everyone can publish msg. – Supawat Pusavanno Sep 29 '14 at 09:58
  • 3
    @tonginbox - We ended up using RabbitMQ, and a "fan-out" strategy, where every consumer has their own queue and messages published to the exchange get replicated to all of them. – Andrew Theken Sep 30 '14 at 12:16

1 Answers1

1

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.

Travis
  • 10,444
  • 2
  • 28
  • 48
  • Before running my sample app (on Windows 7), I installed MSMQ, but didn't configure any queues. When I ran the above app and private queues were created. I'll submit this as a question in the mailing list, but I am sure that MT created those queues. – Andrew Theken Aug 09 '12 at 15:55
  • You might be right about the private queues and it's public queues MT doesn't support. – Travis Aug 10 '12 at 11:51