Why MSMQ is excelent for .NET developers?

Microsoft Message Queuing
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.
1
2
3
4
5
|
public class RequestMessage
{
public string UserId { get; set; }
public string RequestType { get; set; }
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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();
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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;
}
}
|
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.
- MSMQ is only for Windows users.
Original Link:
