Monday, August 27, 2012

Microsoft Message Queuing (MSMQ) Code

This is part 3 out of a 3 part info dump on Microsoft Message Queuing (MSMQ)
    Part #2 Structure
    Part #3 Code


Coding structure concept:
  1. Initialize the queue
  2. Begin listening for new events on the queue OR Send a message to the queue
  3. Close the queue
Main objects in the Systems.Messaging namespace:

MessageQueue
The MSMQ message queue that will receive/send messages.

Message
The actual message or data sent to or read from a queue.
 
Note: All Messages are Objects It's important to understand that the message bodies stored in the queue are actually objects. This is a powerful choice by Microsoft because it allows you to store any type of message, class, structure, and even algorithms, within a message body for transmission.


Interacting with Message Queues:
http://msdn.microsoft.com/en-us/library/ms973860.aspx

#open
Queue Path
Two basic types of queues exist: public and private. While there are slight security differences between using public and private queues, they are for the most part, the same in usage. In code, they only differ by the path you use to connect to the queue. Specifically, two paths are shown below to connect to a public and private queue. The text "Private$" precedes the path for private message queues.

Public Message Queue Path
.\\MyQueue

Private Message Queue Path
.\\Private$\\MyQueue

For more info on Queue Paths see:
http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.path.aspx

 
#sending
Note: BinaryMessageFormatter() allows you to serialize any type of object in the queue's message body and deserialize it back to its original object type.

Note: In a disconnected environment, where the queue cannot be reached at the time the message is sent, it is necessary to use the format name.

Format Name Reference
A format name is a unique identifier for a queue that is generated by MSMQ when the queue is created. The format name can also be generated later by the application. A format name reference is in the form of a string indicating whether a queue is public or private, followed by the GUID for the queue, and other identifiers as necessary.

Note: This is the most efficient way to reference a queue, since MSMQ doesn't have to interpret paths or labels to find the queue.

Path information:
Queue TypeSyntax
Public queueFORMATNAME:PUBLIC=QueueGUID
Private queueFORMATNAME:PRIVATE= MachineGUID\QueueNumber
Journal queueFORMATNAME:PUBLIC= QueueGUID;JOURNAL or FORMATNAME:PRIVATE= MachineGUID\QueueNumber;JOURNAL

e.g.
@"FormatName:DIRECT=TCP:65.55.57.27\private$\test"


Note: Direct format names that specify the HTTP or HTTPS protocol cannot be used to peek at or receive messages, only to send them.


AppSpecific property
The AppSpecific property is an integer unused by Windows and is available to the developer.

This property is often used by developers in the case of messages possibly failing and being re-tried, in which case it is used to count how many times the message has been sent. After the value reaches a certain threshold, the message may be posted to the Dead-letter messages queue located under the "System Queues" folder in Computer Management.


#retrieving
Receiving Messages from the Queue
Receiving messages generally happens asynchronously in Microsoft Message Queue MSMQ. You add a ReceiveCompleted event to the queue and begin listening for new messages to arrive. When a message arrives, your event is triggered, passed a message object, and you can then deserialize the body and process the contents. Your final call in the ReceiveCompleted function would be to start listening again for new messages


Peek()
Returns without removing (peeks) the first message in the queue referenced by this MessageQueue.

Note: The Peek method is synchronous, so it blocks the current thread until a message becomes available or the specified time-out occurs.


Receive()
With the Receive() method, a single message is read and removed from the queue.
Receive() message behaves synchronously and waits until a message is in the queue if there is none.


The Message Loop
"queue.BeginReceive()". This is a critical line to a successful implementation of Microsoft Message Queue in that it provides the means for continuous listening on the message queue. Each time a message is received, the listening process stops. This helps provide a thread-safe environment. However, it also means it's the developer's responsibility to resume listening to the queue.


#closing
Note: Since the message queue object implements the IDisposable interface, we can utilize the "using" statement to automatically close and dispose of the queue for us.
using (queue = new MessageQueue(".\\MyQueue"))


Official Microsoft Code Samples:
How to write to and read from Microsoft Message Queuing in Visual C# - http://support.microsoft.com/kb/815811

Sending Messages to Multiple Destination Examples - http://msdn.microsoft.com/en-us/library/windows/desktop/ms701536(v=vs.85).aspx



The content of this series was found merged and customised from the following sites:
http://support.microsoft.com/kb/178517
http://support.microsoft.com/kb/815811
http://technet.microsoft.com/en-us/library/cc730994
http://www.primaryobjects.com/CMS/Article77.aspx
http://en.wikipedia.org/wiki/Microsoft_Message_Queuing
http://www.c-sharpcorner.com/UploadFile/rajkpt/101262007012217AM/1.aspx
http://www.techrepublic.com/article/using-message-queue-services-in-net/6136686
http://technet.microsoft.com/en-us/library/154e24ed-e149-4a2b-85cc-0dbae721cf48
http://msdn.microsoft.com/en-us/library/system.messaging.message.messagetype.aspx
 

Friday, August 17, 2012

Microsoft Message Queuing (MSMQ) Structure

This is part 2 out of a 3 part info dump on Microsoft Message Queuing (MSMQ)
    Part #2 Structure
    Part #3 Code


MessageQueuing (3)MessageTypes:

Normal
Which is either a typical message sent from an application to a queue, or a response message returned to the sending application.

Acknowledgement
Which Message Queuing generates whenever the sending application requests one. For example, Message Queuing can generate positive or negative messages to indicate that the original message arrived or was read. Message Queuing returns the appropriate acknowledgment message to the administration queue specified by the sending application.

Report
Which Message Queuing generates whenever a report queue is defined at the source Queue Manager. When tracing is enabled, Message Queuing sends a report message to the Message Queuing report queue each time the original message enters or leaves a Message Queuing server.


MessageQueuing (4)QueueTypes:

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.


MessageQueuing (18)Features:

Subqueues
New in Message Queuing 4.0, subqueues are implicitly created local queues that are logical partitions of a physical queue. Applications can use subqueues to group messages. The subqueues feature enables you to logically group messages in a queue without creating another physical queue.

Move messages
New in Message Queuing 4.0, move message functionality allows you to move messages. You can move messages between subqueues of the same main queue, or from a main queue to its subqueue. You cannot move messages from a main queue to a subqueue of a different main queue or between two main queues or between two subqueues of different main queues. The message is moved as is, from the source queue to the target queue. The properties of the message remain unchanged, except for the current move count.

Transactional remote receive
New in Message Queuing 4.0, transactional remote receive is a transactional receive of a message from a remote queue. Transactional remote receive capability may simplify or enable certain message processing scenarios. For example, when work orders from a remote central queue need to be processed across a farm of application servers, a transactional remote receive will enable the message processing to be load-balanced across the server farm.

Delivering messages over HTTP transport
SRMP (SOAP Reliable Messaging Protocol), an XML-based messaging protocol, can be used for delivering high Quality of Service (QoS) messages. The direct, public, and private format names of administration and response queues can be included in messages sent over HTTP transport. Similarly, the names of administration and response queues in HTTP format can be included in messages sent over ordinary (non-HTTP) transport.

Triggers
Triggers provides a mechanism that associates the arrival of each incoming message at a queue with a response that depends on the contents of the message and may invoke either a COM component or a stand-alone executable program. Business rules can be defined and invoked in response to such messages without any additional programming.

Sending messages to multiple destinations
MSMQ clients are able to send the same message to multiple recipient queues. Lists of destination queues can be specified explicitly by means of distribution group objects (distribution lists) in Active Directory Domain Services as well as in the form of multiple-element format names. Support for ensuring that messages sent to distribution lists and multiple-element format names will reach queues on downlevel computers is provided. In addition, message delivery to IP multicast destinations using the PGM protocol is supported.

Message lookup
Message Queuing 4.0 provides a way to peek at or retrieve a specific message without using cursors to navigate through the queue until the message sought is located. This functionality is based on a 64-bit lookup identifier that is assigned to each message when it is placed in a destination queue.

Directory Service Integration
Message Queuing 4.0 provides functionality to integrate with Active Directory Domain Services to store all configuration, security, and status information. Message Queuing clients use the Lightweight Directory Access Protocol (LDAP) to access domain controllers and global catalog servers for Message Queuing-specific information in Active Directory Domain Services directly without assistance from a Message Queuing server on a domain controller.

Workgroup support
Message Queuing can be installed in workgroup mode on computers belonging to a Windows Vista®, Windows Server® 2008 or Windows Server 2003 workgroup, rather than to a domain. In addition, a computer on which Message Queuing is installed in a workgroup can later join a domain, and then separate from the domain.

Active/active cluster support
Message Queuing 4.0 fully supports the active/active paradigm in a server cluster, which means that Message Queuing can run on all nodes in a server cluster simultaneously. Message Queuing triggers are also integrated with active/active cluster support.

Windows CE support
A special version of a Message Queuing client is preinstalled on handheld and palm-sized computers running the Windows CE 3.0 (or later) operating system. Windows CE supports the Message Queuing SRMP protocol, for HTTP messaging.

Message backup and restore
Message storage files, log files, transaction log files, and registry settings can be backed up and restored in case of computer failure.
Message prioritization
Message prioritization allows urgent or important messages to be sent before less important messages, so you can guarantee adequate response time for critical applications at the expense of less important applications.

Guaranteed message delivery
Messages can be stored on a disk-based queue, and then later forwarded to provide guaranteed delivery.

Sending messages within transactions
Using transactional capabilities, you can couple several related actions in a single transaction, ensure messages are delivered in order, ensure messages are delivered only once, and confirm that messages were successfully retrieved from the destination queue.

Dynamic queue creation
You can create queues or change queue properties on the fly without affecting messaging applications.

Message routing
Message Queuing provides message routing based on the physical topology of the network, session concentration needs, and transport connectivity. Session concentration facilitates the efficient usage of slow communication links.

Tuesday, August 7, 2012

Microsoft Message Queuing (MSMQ) Overview

This is part 1 out of a 3 part info dump on Microsoft Message Queuing (MSMQ)
   Part #2 Structure
   Part #3 Code
 
 
MessageQueuing Description:
Message Queuing is a part of the Windows Operating System, also known as MSMQ. It is a messaging infrastructure and a development tool for creating distributed messaging applications for Microsoft Windows. Applications developed for Message Queuing send messages to queues, which are temporary storage locations, from which messages can proceed to their final destination as conditions permit. Such applications can communicate across heterogeneous networks and can send messages between computers that might be temporarily unable to connect to one another. With Message Queuing, end users can communicate across networks and computers that might be offline, regardless of the current state of the network and computers. System administrators can use Message Queuing to efficiently manage large, complex networks of computers and message queues.

Note: The message component of the queue's name often confuses people as they are accustomed to dealing with mail messages, but the message part of a message queue can be any object / piece of data. Messages can be stored as plain text, XML, binary, or even a custom selected format.


MessageQueuing Benefits:
  • Guaranteed message delivery even on disconnected environments.
  • Improved security and optional integrated encryption.
  • Support for sending messages within transactions.
  • Supports sending multicast messages.
  • Priority-based messaging.
  • Efficient routing.
  • Express mode, messages can be sent very fast. (Only stored in memory)
MessageQueuing Installation and Management:
 
Windows 7
You install and uninstall Message Queuing by using Programs and Features in Control Panel.
 
Windows Server 2008 R2
You install and uninstall Message Queuing by using the Add Features Wizard available in Server Manager. 

Earlier OS versions
Start->Settings->Control Panel->Administrative Tools->Computer Management.
Click the plus sign next to Services and Applications. You should see an item called Message Queuing. If you need to install MSMQ, you can do so by entering the Add Remove Programs window, click Add Remove Windows Components, and checkmark the option for Message Queuing.
 
For more installation information and setting permissions in Active Directory Domain Services before installing the Routing Service or the Directory Service Integration features see Install Message Queuing.
 
Note: To manually empty the queue, right-click on "Queue messages", select All Tasks, and select Purge.
 
 
MessageQueuing (6)Subcomponents:

Message Queuing Server
This feature is the core component of Message Queuing, which enables you to perform basic Message Queuing functions.

Directory Service Integration
This feature enables publishing of queue properties to Active Directory, out-of-the-box authentication and encryption of messages using certificates registered in Active Directory, and routing of messages across sites.

Triggers
This feature enables the invocation of a COM component or an executable depending on the filters that you define for the incoming messages in a given queue

HTTP Support
This feature enables the sending and receiving of messages over HTTP.

Multicasting Support
This feature enables queuing and sending of multicast messages to a multicast IP address.

Routing Service
This feature routes messages between different sites and within a site.

Note: When Message Queuing is installed, the Message Queuing Service must be started. This service reads and writes messages and communicates with other Message Queuing servers to route messages across the network.


MessageQueuing (3)Ports:
The following ports are used for Microsoft Message Queuing (MSMQ) operations:
  • TCP: 1801
  • RPC: 135, 2101*, 2103*, 2105*
  • UDP: 3527, 1801
Note: While Message Queuing 3.0 and later uses the same ports that are used in earlier versions of MSMQ, Message Queuing also introduces TCP port 389. TCP port 389 must be open for MQIS queries to be made directly against Active Directory. Additionally, HTTP messaging in Message Queuing 3.0 and later requires that the port specified for the Message Queuing virtual directory be open.


Clustering and Load-balancing MSMQ:
Although Microsoft does not recommend it, it can be done. The following 2 posts will try to discourage you to do so, but also points one into the right direction.
 
[Transactional messages]
http://blogs.msdn.com/b/johnbreakwell/archive/2008/11/18/oil-and-water-msmq-transactional-messages-and-load-balancing.aspx

[Tip 8]
http://www.devx.com/enterprise/Article/22314/0/page/3
 
Update (2012-08-27):
In MSMQ 3.0, the queue's FormatName property lets you specify a public format name, private format name, distribution list format name, multicast address format name, multiple-element format name, or a queue alias.

A multi-element format name is a concatenation of any number of comma-delimited public format names, private format names, direct format names, multicast address format names, or distribution list format names. A MULTICAST formatname allows you to efficiently send a single copy of message such that it is received by multiple recipient queues while leveraging network support for reliable IP multicast. Direct format names, such as DIRECT=HTTP://URLAddressSpecification/msmq/QueueName let you reference public or private queues without accessing the directory.


Note: Be aware of the security implications of multicasting. Messages cannot be encrypted, and messages could be read or used by unauthorized persons. Ensure that the address specified is used only for Message Queuing multicasting.



The content of this series was found merged and customised from the following sites:
http://support.microsoft.com/kb/178517
http://support.microsoft.com/kb/815811
http://technet.microsoft.com/en-us/library/cc730994
http://www.primaryobjects.com/CMS/Article77.aspx
http://en.wikipedia.org/wiki/Microsoft_Message_Queuing
http://www.c-sharpcorner.com/UploadFile/rajkpt/101262007012217AM/1.aspx
http://www.techrepublic.com/article/using-message-queue-services-in-net/6136686
http://technet.microsoft.com/en-us/library/154e24ed-e149-4a2b-85cc-0dbae721cf48
http://msdn.microsoft.com/en-us/library/system.messaging.message.messagetype.aspx