Why MSMQ is excelent for .NET developers?

September 8, 2009 | by Maciej Greń

Comments
Microsoft Message Queuing

Microsoft Message Queuing

In my last post “Why Apache ActiveMQ isn’t good for .NET developers?” I described the situation when I couldn’t solve my issue using ActiveMQ. Therefore, I tested another message queue system – Microsoft Message Queuing (MSMQ). Because this system is a Microsoft’s product, it is fully supported by .NET Framework. I don’t know why I was prejudiced against this system in the beginning. In fact, it is a useful and simple message queue system for any .NET developer. Why? See below.

MSMQ  at a glance

Microsoft Message Queuing (MSMQ) is a message queue implementation developed by Microsoft and deployed in its Windows operating systems since Windows NT 4 and Windows 95. Current version 4.0 is released with Windows Vista and Windows Server 2008. It is a very mature system and has a long list of features, like: triggers, connectionless messaging, guaranteed message delivery and many others.

For me a very important function is message peeking. In the below example I will show how it works.

Why is it so good? – Example

To answer this question I demonstrate how simple I made my non-trivial system. My issue was to make sample applications: two clients and a server, which are communicating by messages. Detailed description of this issue is in my previous post. I made this system using MSMQ and .NET Framework 3.5 in 5 steps.

1. Installing MSMQ in your Windows system.

In Vista: Open “Control Panel -> Programs ->Turn Windows features on or off”. Find and check “Microsoft Message Queue(MSMQ) Server”.

MSMQ_install

2. Creating message class

I make a class that defines the message structure. You can define any class and send it by MSMQ.

  1. public class RequestMessage
  2.  {
  3.      public string UserId { get; set; }
  4.      public string RequestType { get; set; }
  5.  }

3. Creating Client 1 class

This class creates queue if it doesn’t exist and sends message to it.

  1. class MSMQSender
  2.  {
  3.      public void SendMessage(string userId, string requestType)
  4.      {
  5.          private string queuePath = @".\Private$\test";
  6.          MessageQueue mq;
  7.  
  8.          // open or create message queue
  9.          if (MessageQueue.Exists(queuePath))
  10.              mq = new MessageQueue(queuePath);
  11.          else
  12.              mq = MessageQueue.Create(queuePath);
  13.  
  14.         Message message = new Message();
  15.         message.Recoverable = true;
  16.  
  17.         RequestMessage rm = new RequestMessage();
  18.         rm.UserId = userId;
  19.         rm.RequestType = requestType;
  20.  
  21.         message.Body = rm;
  22.  
  23.         mq.Send(rm);
  24.  
  25.      }
  26.  }

4. Creating Server class

Class creates or open queue receives messages and processes them by worker object. Worker can only process single message. It returns control to server object when processing is done.

  1. class Server
  2.  {
  3.       public Server()
  4.       {
  5.            MessageQueue mq;
  6.  
  7.            // open or create message queue
  8.            // …
  9.  
  10.            // set type of message
  11.            ((XmlMessageFormatter)mq.Formatter).TargetTypes = new Type[] { typeof(RequestMessage) };
  12.  
  13.            while(true)
  14.            {
  15.                Message message = mq.Receive();
  16.                RequestMessage rm = (RequestMessage)message.Body;
  17.  
  18.                Worker worker = new Worker(rm.UserId, rm.RequestType);
  19.                worker.DoJob();
  20.            }
  21.       }
  22.  }

5. Creating Client 2 class

Class reads all messages in the queue, checks its content and adds it to the list . It is a simple queue monitor.

  1. class MSMQReceiver
  2.  {
  3.      public List<string> ReadFromQueue(string userId)
  4.      {
  5.          // open or create message queue
  6.          // …
  7.  
  8.          // set type of message
  9.  
  10.          Cursor cursor = mq.CreateCursor();
  11.  
  12.          Message m = mq.Peek(new TimeSpan(1), cursor,
  13.                             PeekAction.Current);
  14.  
  15.          RequestMessage rm = (RequestMessage)m.Body;
  16.  
  17.          rm.UserId == userId)
  18.              list.Add(rm.RequestType);
  19.  
  20.          while ((m = mq.Peek(new TimeSpan(1), cursor,
  21.                  PeekAction.Next)) != null)
  22.          {
  23.              rm = (RequestMessage)m.Body;
  24.  
  25.              if (rm.UserId == userId)
  26.                  list.Add(rm.RequestType);
  27.          }
  28.  
  29.          return list;
  30.     }
  31.  }

And that is all. Application works great and does exactly what I wanted. After working with ActiveMQ NMS API I didn’t expect that making this system would go so easily and fast. I’m delighted.

Summary

The main advantages for me are:

  • API is very simple – in several lines of code I have a wide access to queue system.
  • API is well documented (by MSDN).
  • I don’t need any additional server or library – I need only Windows and .NET Framework.
  • Great thing is also that I can send any kind of objects and even streams.
  • I can also look up what is going on with my queues. By “Computer Management” tool in Control Panel as well as by code.
  • It is free.

Main disadvantage:

  • MSMQ is only for Windows users.

Share your opinion and experience with us below or meet us on Twitter: @GOYELLO.

Thanks to Karol Świder for helping me to write this blog post.

  • Eugenis
    ActiveMQ can be very easy as well. You need to look at Spring .NET Spring NMS.
  • Mark
    I wouldn't say MSMQ is excellent at all. But rather it is your only choice if you are a pure Microsoft "Shop" [I despise that term]. I tried using it a few years ago with .Net to do some triggering I think. I couldn't get it to work whatever it was.

    Using ActiveMQ is the correct choice if you work in a mixed environment. If not, either learn something new or stick with what Bill gives you. :(.
  • maciejgren
    Hi,
    thank you for your comment. I understand your opinion but I have to disagree. Like you said you used it few years ago. I can imagine that then MSMQ offered less functionalities than now. After my review of other systems I had to conclude that the best is to return to the "what Bill gives me" because other systems offered more but also limited me in areas that I wanted to have control.

    For sure ActiveMQ has their own placement in the market where it is better than using pure MSMQ. It is important to know that the decision has to be careful because you not only gain, but lose as well.
  • Mark
    Not sure we disagree. And I don't have an opinion. I have experience.

    I'd hope that MSMQ does more. But I have used their products for years and MSMQ is one thing that doesn't seem to be important to their users nor them, sadly. But even more sad, it is not alone in this regard.

    If you can stay within the "gutter-bumpers" that Microsoft provides then all is well. But I never can seem to be able to do that. :(
blog comments powered by Disqus