Why MSMQ is excelent for .NET developers?

by Maciej Greń in Best practices,GOYELLOblog,Microsoft

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.
  • 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. :(

  • Eugenis

    ActiveMQ can be very easy as well. You need to look at Spring .NET Spring NMS.

  • http://www.world-wide-villas.com Algarve Villa Rentals

    Nice Post I already digged this

    respect
    ricky marten
    ______________________________________________

  • Pingback: prgvlnby

  • http://www.air-jordan-17.com air jordan 17

    It looks good,I have learn a recruit!
    Recently,I found an excellent online store, the “http://www.air-jordan-18.com are completely various, good quality and cheap price,it’s worth buying!

  • http://www.discount-nike-dunk-shoes.com nike dunk sb shoes

    I totally agree the standpoint of upstairs, and I believe this will be a trend. I often come this forum , rom here I learn much and know the newest tide! the content here constantly update shoe and I love it! Another I know some websites which often update their contents, you guys should browse if you are http://www.assure-every.com” rel=”nofollow”>free.http://www.assure-every.com

  • http://www.prada-outlet-store.com prada outlet

    Hhe article's content rich variety which make us move for our mood after reading this article. surprise, here you will find what you want! Recently, I found some wedsites which commodity is research-laboratory colorful of fashion. Such as that worth you to see. Believe me these websites won’t let you down.

  • http://pulse.yahoo.com/_B6YUTCEMRVUAXIIBU3RQMA2APM Holt

    I have a long history with MSMQ and unfortunately, have grown to dislike it immensely. As of MSMQ 3.0 they switched to using kernal memory for MSMQ and the result is a very unforgiving blue screen if your message store gets too large. You can set limits on the store size to avoid this, but in my opinion, it still gives you less storage than I would like in a high transaction environment. For instance we pump 20+ million of messages through our queues a day. If there is a failure somewhere and we get backups of 400k messages (which can happen quickly), we may experience a server crash or at least MSMQ gets very, very s-l-o-w. It's too bad the size of the message store actually makes performance degrade because it's really the last thing you want to have happen when your system starts to queue up. This is why we're moving to ActiveMQ. It provides much much greater flexibility and capacity.

  • Rathi Emails

    Good Article very useful,thank you.