Is Lean software development better than Scrum?
Quite some years ago, after attending a presentation about lean production at Toyota, I decided to buy the ...
Why MSMQ is excelent for .NET developers?
CommentsIn 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”.

2. Creating message class
I make a class that defines the message structure. You can define any class and send it by MSMQ.
-
public class RequestMessage
-
{
-
public string UserId { get; set; }
-
public string RequestType { get; set; }
-
}
3. Creating Client 1 class
This class creates queue if it doesn’t exist and sends message to it.
-
class MSMQSender
-
{
-
public void SendMessage(string userId, string requestType)
-
{
-
private string queuePath = @".\Private$\test";
-
MessageQueue mq;
-
-
// open or create message queue
-
if (MessageQueue.Exists(queuePath))
-
mq = new MessageQueue(queuePath);
-
else
-
mq = MessageQueue.Create(queuePath);
-
-
Message message = new Message();
-
message.Recoverable = true;
-
-
RequestMessage rm = new RequestMessage();
-
rm.UserId = userId;
-
rm.RequestType = requestType;
-
-
message.Body = rm;
-
-
mq.Send(rm);
-
-
}
-
}
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.
-
class Server
-
{
-
public Server()
-
{
-
MessageQueue mq;
-
-
// open or create message queue
-
// …
-
-
// set type of message
-
((XmlMessageFormatter)mq.Formatter).TargetTypes = new Type[] { typeof(RequestMessage) };
-
-
while(true)
-
{
-
Message message = mq.Receive();
-
RequestMessage rm = (RequestMessage)message.Body;
-
-
Worker worker = new Worker(rm.UserId, rm.RequestType);
-
worker.DoJob();
-
}
-
}
-
}
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.
-
class MSMQReceiver
-
{
-
public List<string> ReadFromQueue(string userId)
-
{
-
// open or create message queue
-
// …
-
-
// set type of message
-
-
Cursor cursor = mq.CreateCursor();
-
-
Message m = mq.Peek(new TimeSpan(1), cursor,
-
PeekAction.Current);
-
-
RequestMessage rm = (RequestMessage)m.Body;
-
-
rm.UserId == userId)
-
list.Add(rm.RequestType);
-
-
while ((m = mq.Peek(new TimeSpan(1), cursor,
-
PeekAction.Next)) != null)
-
{
-
rm = (RequestMessage)m.Body;
-
-
if (rm.UserId == userId)
-
list.Add(rm.RequestType);
-
}
-
-
return list;
-
}
-
}
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
-
Mark
-
maciejgren
-
Mark


