Search code examples
c#msmq

Understanding the basics of MSMQ


I am just starting on MSMQ and so far it doesn't look particularly complicated. I've worked through some articles on MSDN and I've managed to implement a producer which delivers messages to a private queue on a remote machine and a consumer on the remote machine that processes those messages. No problems.

Now I just want to make sure I understand the basic workings of MSMQ (or queues in general I suppose) before I go forward.

First of all, is there a concept of specifying the recipient of a message? In other words, if I have two consumers polling the same queue, will each one just grab whatever message is in front of the queue and process it or can the producer specify which consumer a specific message is intended for? What keywords should I Google or look for on MSDN to read more about this?

Secondly, is there a concept of sending messages which are to be processed by all consumers, not just the first one to receive? As a simple example, let's say the company's receptionist has a producer on her computer with which she can notify the rest of the company (by pushing a message onto a queue) that the sandwich lady has arrived. Any interested employees will have a consumer running on their computers which will see that message, but not remove it so that the other consumers can also see it. Again, what do I Google for to read more about this?

Finally, does every implementation always consist of one or more producer(s) and one or more consumer(s)? What if you have only two machines that need to send messages to a fro? Do you typically set up two queues (A and B) and on machine 1 you implement a producer for queue A and consumer for queue B while machine 2 gets a producer for queue B and a consumer for queue A or are there better ways of doing this?


Solution

  • First of all, is there a concept of specifying the recipient of a message?

    No, there is no routing or other behavior which can provide this. The producer and consumer are fully decoupled, and MSMQ doesn't support any kind of conditional message dequeuing.

    ...is there a concept of sending messages which are to be processed by all consumers, not just the first one to receive?

    Again, no there is no built in "publisher" functionality.

    ...does every implementation always consist of one or more producer(s) and one or more consumer(s)?

    Yes, that is correct. MSMQ only has native support for uni-directional messaging so any messaging endpoints involved in the message exchange will need a queue to read off and a queue to send to. By convention, a messaging exchange participant will send to a remote queue and receive from a local queue.

    I guess the questions you are asking are indicative of an expectation that a queueing platform should provide more than one-way, asynchronous messaging, but I would disagree. MSMQ is more of a transport layer than a fully fledged messaging system, but it's a very solid, reliable foundation upon which to build the kind of enterprise features you are after.

    As an example, WCF provides a request/response wrapper around MSMQ, which allows bi-directional communication.

    NServiceBus brings both request/response and publish/subscribe messaging patterns to MSMQ (although this is a pay-for product at high messaging volumes/scale-out).

    If you want to consider other messaging systems which have more of these enterprise features built in (but don't use MSMQ) you could look at Rabbit, or Azure Service Bus