Using message queue services in .NET
Takeaway: The combination of Microsoft Message Queuing's inclusion with Windows and the .NET System.Messaging namespace makes it easy to utilize messaging in your .NET application. Tony Patton offers more details about using message queues.
Developers are often faced with scenarios where operations need to be performed asynchronously (i.e., processes are initiated without waiting for the operation to complete). The message queue fills this need by providing a central location or pool where you can place or remove data. An application can place data in the queue and continue with its business while another application grabs the data when it is ready.
The message component of the queue's name often confuses developers as they are accustomed to dealing with mail messages, but the message part of a message queue can be any piece of data. It provides guaranteed message delivery for your applications.
This week, I explain how you can take advantage of message queuing via Microsoft Message Queuing (MSMQ) and the .NET Framework.
Why use message queuing?
You may be thinking that you can easily use message queuing via a simple database table where one application writes data to it and another reads it. Message queue platforms are more robust, as they often provide their own security, transactional support, and more. A key aspect is routing capabilities to deliver a message. There are various message queue platforms that come with MSMQ.
What is MSMQ?
MSMQ is a component of Windows 2000, Windows XP, Windows Server 2003, and will be included in Windows Vista and future server versions of Windows. It allows cooperating applications to send and receive messages to each other—even if the intended recipient application is not running, or the computer on which the sender or recipient application is running is disconnected from the network. Messages are stored and forwarded by MSMQ until they reach the destination queue. A recipient application can retrieve data from the queue.
The key feature of message queues like MSMQ is the fact that it decouples sender and recipient applications so they do not need to run at the same time. This means that an application can place data in the queue and not deal with whether the item on the queue is delivered to the recipient.
MSMQ is available as an optional Windows component, so it requires a little extra effort to install via the Add Or Remove Windows Components wizard in the Windows control panel. MSMQ can be deployed in two modes: domain or workgroup (which only uses private queues). With MSMQ installed, it is readily available for use within .NET applications.
MSMQ interaction
Developing messaging-based applications begins with a queue. MSMQ includes four queue types:
- Outgoing: Used to temporarily store messages before they are sent to its destination.
- Public: Published in the Active Directory. Applications on different servers throughout the network can find and use public queues via Active Directory.
- Private: These queues are local to a server and are not available to other machines (thus, these queues are not published in Active Directory).
- System: Contains journal messages (sent from the system), dead messages, and transactional dead-letter messages. Dead messages are undeliverable.
Programmatic manipulation of MSMQ is available via the Systems.Messaging namespace. There are two main objects in this namespace:
- Message: The actual message or data sent to or read from a queue.
- MessageQueue: The MSMQ message queue that will receive/send messages.
Programming MSMQ
The first step when working with MSMQ is creating a queue. You can do this via the Message Queuing selection within the Computer Management console in Windows, or you can create a queue programmatically. In Listing A, the C# code creates a new private MSMQ message queue (if it does not already exist) and creates a message with it.
The code uses the Exists method of the MessageQueue class to determine if the private queue called TechRepublic exists. If it does exist, it instantiates the MessageQueue object with the existing queue; otherwise, a new queue is created.
A new Message object is used to send a message to the queue. Its priority is set to normal. Its Label property specifies the messages title displayed in the MSMQ console with the body containing the meat of the item placed on the queue. In this case, I am only sending text, but you can use any type of object. The Send method of the MessageQueue class sends the message to the queue. Listing B contains the equivalent VB.NET code.
The next step is reading the message from the queue. It is a simple process with the Receive method of the MessageQueue class. The Receive method returns a message object if there are any messages in the queue; otherwise, it waits for a message to appear (you can set a timeout period). Retrieving an object from the queue requires you to know its type.
The MessageQueue class' Formatter property allows you to easily specify the type of object being retrieved. In my simple example, text is being used, so System.String is used. In Listing C, the C# code reads messages from my test queue.
The TimeSpan object passed to the Receive method specifies how long the system will wait before an exception is thrown. The next step is setting the Formatter method—in this example, the object is converted to string to read the previously stored text. The Receive method actually reads the message from the queue, and its values are displayed in the console. The queue is closed in the finally portion of the try block.
Easily use messaging
The combination of MSMQ's inclusion with Windows and the .NET System.Messaging namespace makes it easy to utilize messaging in your .NET application. A message queue provides a powerful way to asynchronously send and receive messages (data) in enterprise applications.
Miss a column?
Check out the .NET Archive, and catch up on the most recent editions of Tony Patton's column.
Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.
Print/View all Posts Comments on this article
|
|
|
|
|
|
White Papers, Webcasts, and Downloads
- Tom Davenport Study: Linking decisions and information for organizational performance IBM Tom Davenport's new client study looks at approaches to linking ... Download Now
- Building the Virtualized Enterprise with VMware Iinfrastructure VMware VMware virtualization software has been adopted by over 120,000 enterprise ... Download Now
- The Impact of Virtualization Software on Operating Environments VMware Today's use of virtualization technology allows IT professionals to ... Download Now
- VMware Infrastructure: A Guide to Bottom-Line Benefits VMware Frustrated by the high cost of maintaining or building ever-larger data centers? Get the facts you need to formulate your Virtualization Action Plan. Download Now
- Why Isn't Server Virtualization Saving Us More? A Few Small Changes May Dramatically Increase Your Efficiency VMware Ever wonder why your company isn't saving more from its server virtualization? Making a few small changes could dramatically increase your efficiency. Download Now
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET

