http://www.developer.com/net/vb/article.php/3297911/Using-Message-Queues.htm
Welcome to the next installment of the CodeGuru .NET Nuts & Bolts column, and the first column article of the New Year. In this column, we'll explore the use of message queues and how they can be useful to us in our applications. This will involve using classes in the System.Messaging namespace. Message queuing is a communication tool that allows applications to reliably interconnect in a distributed environment where one of the applications may or may not be available at any given time. The queue acts as a holding container for messages as they are sent between applications. The applications send messages to and read messages from queues to communicate back and forth. An application writes a message to a queue, which will then be received and processed by another application at some point determined by the receiving application. This type of communication is designed for asynchronous use where the applications involved are not waiting for an immediate response from the other end. Microsoft Message Queue (MSMQ) is a queuing product that was originally introduced along with Microsoft Transaction Server (MTS) as a part of the Option Pack for Windows NT 4.0. It is now known as Windows Message Queuing and is included with the Windows Server products rather than as an add-on. It can be setup through Add/Remove Programs -> Add/Remove Windows Components -> Message Queue. It can operate in a standalone fashion where only the server on which it is installed may access any of the queues, or it can exist in an Active Directory (AD)-based environment where queues are made available to those with the proper security credentials. When used in an AD environment, a Message Queue network can be established that allows servers to send messages to each other and perform various roles within the queuing network. It does not require any additional licensing for use. There are two different types of queues. There are queues that are system generated and those that are user generated. For the sake of this article, I'll focus solely on the user-generated types. The two main types are as follows: To follow the examples contained in this article, you will need to establish a private queue on your local computer. The Message Queuing can be accessed under Services and Applications in the Computer Management console. The following illustration depicts a queue, called MyPrivateQueue, that will be used for all of the examples below. I assigned Everyone Full Control access to the queue by right-clicking, selecting Properties, and then the Security tab. Much like a database connection, the type of queue dictates the connection string used to make a connection to a particular queue. To access a public queue, the connection is made by MachineName\QueueName. To access a private queue, the connection is made by MachineName\Private$\QueueName. When accessing a queue on a local machine, the MachineName variable may be replaced by the "." character. A connection can be initiated as follows: The classes in the System.Messaging namespace make it relatively simple to write messages to a queue. First, you create a connection to the appropriate message queue by using the MessageQueue class, and then specify the message to send by using the Send method. The messages can take the form of something simple such as text or a string containing XML, or something more complex that is created through an instance of the Message class. The following sample demonstrates writing a message to a private queue on the local computer. To execute this code, you must have first created a private queue and granted the appropriate security permissions to write to the queue. If you check the private queue contents through the Computer Management console, you will notice a new message entered into your queue, that looks something similar to the following: The Send method will write the message in an XML format by default. Other options are available by setting the Formatter on an instance of the Message class. Just as the classes in the System.Messaging namespace make it simple to write messages to a queue, it is equally as easy to read messages from a queue. First, you create a connection to the appropriate message queue by using the MessageQueue class. Next, you specify a formatter to retrieve the message. Then, create an instance of a Message class and use its Receive method to pull a message from the queue. If you want to check the contents of the queue without removing the message from the queue, use the Peek method instead. The following sample class demonstrates reading a message from a private queue on the local computer. You will notice that the message is not retained in the queue once it is read by using the Receive method. The following sample class demonstrates peeking into a message queue to examine the message without removing it from the queue. If you examine the queue in Computer Manager, you will notice that the message still exists within the queue. Now we have successfully demonstrated writing to and used multiple ways to read from a message queue. There are all sorts of ways that we could use queues to our advantage in our applications. Here are some ideas for you to consider on your own: The topic of the next column is yet to be determined. If you have something in particular that you would like to see explained here, you could reach me at mstrawmyer@crowechizek.com. Mark Strawmyer, MCSD, MCSE, MCDBA is a Senior Architect of .NET applications for large- and mid-size organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in architecture, design, and development of Microsoft-based solutions. You can reach Mark at mstrawmyer@crowechizek.com. # # #
Using Message Queues
January 12, 2004
What Is Message Queuing?
Where Can I Get a Message Queue?
Queuing Configurations
Types of Queues

Click here for a larger image.Connecting to a Queue
MessageQueue queue = new MessageQueue(".\\Private$\\MyPrivateQueue");Writing to a Queue
Writing to a Queue Sample Code
System.Messaging.MessageQueue queue = newSystem.Messaging.MessageQueue(".\\Private$\\MyPrivateQueue");queue.Send("Hello world");
Reading from a Queue
Reading from a Queue Sample Code
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(".\\Private$\\MyPrivateQueue");queue.Send("Hello world");System.Messaging.Message msg = queue.Receive();msg.Formatter = new System.Messaging.XmlMessageFormatter( new string[] {"System.String"});Console.WriteLine(msg.Body);Peeking into a Queue Sample Code
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(".\\Private$\\MyPrivateQueue");queue.Send("Hello world");System.Messaging.Message msg = queue.Peek();msg.Formatter = new System.Messaging.XmlMessageFormatter( new string[] {"System.String"});Console.WriteLine(msg.Body);Possible Enhancements
Future Columns
About the Author