17 October 2010 CI Team

imageA short time ago I found MSMQ. To say it easy: MSMQ is a system where messages are able to be classified into queues and be converted piece by piece.

Why should I use this?

MSMQ is a system of queues. You put your message into it and than somebody will take it and convert it. This could be very useful in distributed applications for example. So for example it is possible to generate E-Mails, put them in a queue and send them out piece by piece so the server won´t be overloaded. Of course there are a lot more possibilities but this is just an introduction.

Later I´m going to talk about some more advantages and disadvantages and I would be very glad to get some comments from you.

Requirements

The infrastructure is included every ware since XP (?). You only need to activate it in the windows functions:

image

Afterwards you are able to see MSMQ Services in the Computer-Management and Service:

image

Click right and you will be able to look into already existing queues and to create new ones:

image

Here it is also possible to take a look into the several massages:

image

For my demo code it is necessary to create the “test” queue beneath the private queues.

The Code:

In .NET Framework you will find everything you need to start:

image

Write a new message into the queue:

            
MessageQueue queue = new MessageQueue(@".\private$\test");
             queue.Send("test");

Read the message:

MessageQueue queue = new MessageQueue(@".\private$\test");
Console.WriteLine(queue.Receive().Body.ToString());

Easy, isn´t it? If you click on “receive” the first message of the queue will be chosen.

Afterwards the message won´t be reachable for other clients as well.

Extensive file types:

Of course it is possible to send extensive file types as well. These file types will be written into the message by the XmlSerializer.

Here is my Example:

using System;
using System.Collections.Generic;
using System.Messaging;
using System.Text;

namespace MSMQ
{
    public class User
    {
        public string Name { get; set; }
        public DateTime Birthday { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Simple Text");
            Console.WriteLine("Input for MSMQ Message:");
            string input = Console.ReadLine();

            MessageQueue queue = new MessageQueue(@".\private$\test");

            queue.Send(input);

            Console.WriteLine("Press Enter to continue");
            Console.ReadLine();

            Console.WriteLine("Output for MSMQ Queue");
            Console.WriteLine(queue.Receive().Body.ToString());
            Console.ReadLine();

            Console.WriteLine("Complex Text");

            User tester = new User();
            tester.Birthday = DateTime.Now;
            tester.Name = "Test Name";
            queue.Send(tester);

            Console.WriteLine("Output for MSMQ Queue");
            User output = (User)queue.Receive().Body;
            Console.WriteLine(output.Birthday.ToShortDateString());
            Console.ReadLine();

        }
    }
}

It works. And now? WHY should I use MSMQ?

You are right. It is possible to make this whole queue thing with SQL ore other self made applications. So why should you take a look on MSMQ?

Advantages- and Disadvantages: (took it from the stack overflow)

Cons:

  • Each queue can only be 2GB.
  • Each message 4MB (altough the 4MB limit can be fixed by using MSMQ with WCF).
  • Only for Windows so you´re limited to use it with .NET, C/C++ or COM library for COM-enabled environments.

Pros:

  • Supports Windows Network Load Balancer.
  • Supports Microsoft Cluster Service.
  • Integrated with Active Directory.
  • Ships with Windows.
  • Supports transactions.
  • MSMQ messages can be tracked by audit messages in the Windows Event log.
  • Messages can be automatically authenticated (signed) or encrypted upon sending, and verified and decrypted upon reception.

Questions to you…

Do you use MSMQ already? Ore do you prefer a simple SQL data base? Of course the pros sound nice but isn´t this possible with SQL as well? Are you having any more extensive examples?

In my opinion it isn´t easy to clarify which option is the better one but maybe some of you know about some additional frameworks ore some absolutely no-go´s? ;)