Category Archives: MSMQ

MSMQ

Funnily enough I’ve never used MSMQ in a project having spent most of my time using TIB RV and ActiveMQ, but time to change that. This is a quick start to getting up and running with the key parts of MSMQ.

First off, to use MSMQ from a .NET project add a reference to System.Messaging.

Create a queue

To create a queue programatically use the following (this sample creates a private queue named test)

const string QUEUE_NAME = @".\private$\Test";

if(!MessageQueue.Exists(QUEUE_NAME))
{
   MessageQueue.Create(QUEUE_NAME);
}

Note: If you try to create the queue when it already exists you will get a MessageQueueException – A queue with the same path name already exists. Hence the use of the MessageQueue.Exists method.

Deleting a queue

To delete the queue we’ve just created we can use the following

const string QUEUE_NAME = @".\private$\Test";

if(MessageQueue.Exists(QUEUE_NAME))
{
   MessageQueue.Delete(QUEUE_NAME);
}

Note: If you try to delete a queue that does not exist, you’ll get a MessageQueueException – The queue does not exist or you do not have sufficient permissions to perform the operation. Hence the use of the MessageQueue.Exists method.

Sending a message

// we'll assume the queue exists, so won't repeat creation of it
MessageQueue messageQueue = new MessageQueue(QUEUE_NAME);
messageQueue.Formatter = new XmlMessageFormatter(new[] { typeof(string) });

messageQueue.Send(new Message("Hello MSMQ World");

Note: Without the formatter any attempt to recive this message will fail with a “Cannot find a formatter capable of reading this message” exception.

Receiving a message

Note: MSMQ does not have an event or callback receive method. A call to Receive will block the current thread.

// we'll assume the queue exists, so won't repeat creation of it
MessageQueue messageQueue = new MessageQueue(QUEUE_NAME);
messageQueue.Formatter = new XmlMessageFormatter(new[] { typeof(string) });

Message msg = messageQueue.Receive();
Console.WriteLine(msg.Body);

As per the sending message sample, we need to set the formatter so we can deserialize the message body. As stated, Receive will block the current thread. We can place a timeout on the Receive call if we want to return control to the thread after an amount of time or simply block (in which case we’d be best running the Receive code from a separate task/thread.

Queue paths/names

The queue path is made up of the machine name (or . for local machine) then a backslash followed by a path to the queue (see MessageQueue Constructor for a full explanation).

I’ve shown the basic format below (as well)

[table “” not found /]

Creating and Deleting Queues via the UI

Finally for this post, I’ll conclude with the steps to admin. your MSMQ queues from the MMC snap-in UI.

In Windows XP (yes some companies still run XP). From the control panel select Administrative Tools then run the Computer Management applet. Assuming you have MSMQ installed you should see something like the screen shot below

msmq-applet

Right mouse clicking on the folders Private Queues etc. will offer an option to create a new queue and bringing the context menu up on the queue name allows us to delete a queue.