Introduction to Message Queuing and its use

Source: Internet
Author: User
Tags msmq
With MSMQ (Microsoft Message Queue), application developers can easily communicate quickly and reliably with applications by sending and receiving messages. Message processing provides you with secure messaging and a reliable fail-safe method for performing many business processes.

MSMQ, like XML Web services and. Net remoting, is a distributed development technology. However, when using XML Web services or. Net Remoting components, the client needs to exchange information in real time with the server side, and the server needs to remain online. MSMQ can work offline while the server is temporarily saved in Message Queuing on the client side and then sent to server-side processing at a later time.

Obviously, MSMQ is not suitable for clients that require the server side to respond in a timely manner, and MSMQ interacts asynchronously with the server side without worrying about waiting for a lengthy process on the server side.

Although both XML Web services and. Net remoting provide the [OneWay] property to handle asynchronous calls, the server-side long method call is used to prevent long-time blocking of the client side. However, the problem of a large number of client workloads cannot be resolved, at which time the server accepts requests faster than processing requests.

In general, the [OneWay] property is not used in a dedicated messaging service.

1. Basic Terminology and concepts (basic terms and concepts)

A message is a unit of data that is transferred between two computers. Messages can be very simple, such as containing only text strings, or they can be more complex and may contain embedded objects.

The message is sent to the queue. Message Queuing is the container in which messages are saved during the transmission of a message. The Message Queue Manager acts as an intermediary when relaying a message from its source to its destination. The primary purpose of the queue is to provide routing and guarantee the delivery of messages, and if the recipient is unavailable when the message is sent, Message Queuing retains the message until it can be successfully passed.

Message Queuing is Microsoft's message processing technology, which provides message processing and Message Queuing functionality to any application in any combination of computers that have Microsoft Windows installed, whether they are on the same network or whether they are online at the same time.

A Message Queuing network is any group of computers that can send messages back and forth to each other. Different computers in the network play different roles in ensuring that messages are handled smoothly. Some of them provide routing information to determine how to send messages, some hold important information about the entire network, and some just send and receive messages.

During the Message Queuing installation, the administrator determines which servers can communicate with each other and sets special roles for specific servers. The computers that make up this Message Queuing network are called sites, and they are connected to each other through site links. Each site link has an associated "cost" that is determined by the administrator to indicate how often messages are delivered through this site link.

Message Queuing administrators also set up one or more computers on the network as a routing server. The routing server determines how the message is delivered by looking at the cost of each site link, determining the quickest and most efficient way to pass the message through multiple sites.

2. Queue type

There are two main types of queues: Queues and System queues created by you or other users on your network.

A user-created queue may be any one of the following queues:

The public queue is replicated throughout the Message Queuing network and may be accessed by all sites that are connected by the network.

Private queues are not published throughout the network. Instead, they are available only on the local computer where they reside. A private queue can only be accessed by an application that knows the full path name or label of the queue.

The management queue contains messages confirming the receipt of messages sent in a given Message Queuing network. Specifies the management queue (if any) that you want the MessageQueue component to use.

The response queue contains the response message that is returned to the sending application when the message is received by the target application. Specifies the response queue, if any, that you want the MessageQueue component to use.

System-generated queues generally fall into the following categories:

The journal queue Optionally stores a copy of the sent message and a copy of the message that is removed from the queue. A single journal queue on each Message Queuing client stores a copy of the message sent from that computer. A separate journal queue is created for each queue on the server. This journal tracks messages that have been removed from the queue.

The dead-letter queue stores a copy of a message that cannot be delivered or has expired. If the expired or undeliverable message is a transactional message, it is stored in a special dead-letter queue, called a transactional dead-letter queue. Badmail is stored on the same computer as the expired message. For more information about time-out periods and expired messages, see Default message properties.

The report queue contains a message that indicates the route the message has reached to the destination, and can also contain a test message. There can be only one report queue on each computer.

A "dedicated system queue" is a series of private queues that the storage system needs to manage and notify messages that are required to perform message processing operations.

Most of the work done in the application involves accessing the public queue and its messages. However, depending on the application's journaling, acknowledgments, and other special processing needs, several different system queues are likely to be used in daily operations.

3. Synchronous and asynchronous communication (synchronous VS. Asynchronous communication)

Queue traffic is inherently asynchronous, because sending messages to and from a queue is done in a different process. In addition, the receive operation can be performed asynchronously because the person to receive the message can invoke the BeginReceive method on any given queue and then immediately resume other tasks without waiting for a reply. This is quite different from what people know about "synchronous communication".

In synchronous communication, the sender of the request must wait for a response from the intended receiver before performing other tasks. The time the sender waits depends entirely on the time it takes for the receiver to process the request and send the response.

4. Interacting with Message Queuing (interacting with messages Queues)

Message processing and messaging provide a powerful and flexible mechanism for interprocess communication between server-based application components. Compared to direct calls between components, they have several advantages, including:

Stability-component failures affect messages much less than direct calls between components because the messages are stored in the queue and remain there until they are handled appropriately. Message processing is similar to transactional processing because message processing is guaranteed.

Message priority-more urgent or more important messages can be received before relatively unimportant messages, so you can guarantee sufficient response time for critical applications.

Offline capabilities-When messages are sent, they can be sent to a temporary queue and remain there until they are successfully delivered. When access to the desired queue is not available for any reason, the user can continue to perform the operation. At the same time, other operations can proceed as if the message has been processed because the message is guaranteed when the network connection is restored.

Transactional message processing-coupling multiple related messages into a single transaction, ensuring that messages are delivered sequentially, delivered only once, and can be retrieved successfully from their destination queue. If any errors occur, the entire transaction is canceled.

The security-messagequeue component is based on Message Queuing technology that uses Windows security to secure access control, provides auditing, and encrypts and validates messages sent and received by components.

5. Writing a simple message queue program under the. NET Environment

(1) First install message Queuing Services

Install MSMQ by using the Control Panel, "Add/remove Programs" – "Add/remove Windows Components" step.

MSMQ can be installed as workgroup mode or domain mode. If the installer does not find a server running a message queue that provides directory services, it can only be installed as a workgroup mode, and Message Queuing on this computer only supports creating private queues and creating direct connections to other computers running Message Queuing.

(2) Configuring MSMQ

Open Computer Management–message Queuing, create Msmqdemo queue under private queues

(3) Writing code-simple demonstration of MSMQ objects

The MessageQueue class is the wrapper around Message Queuing. The MessageQueue class provides a reference to the Message Queuing queue. You can specify a path to connect to an existing resource in the MessageQueue constructor, or you can create a new queue on the server. Before calling send, Peek, or Receive, you must associate a new instance of the MessageQueue class with an existing queue.

MessageQueue supports two types of message retrieval: synchronous and asynchronous. The synchronous Peek and Receive methods allow the process thread to wait for a new message to arrive at the queue with a specified interval. The asynchronous BeginPeek and BeginReceive methods allow the main application task to continue executing in a separate thread before the message arrives in the queue. These methods work by using callback objects and state objects so that information can be communicated between threads.

Send Message

private void Btnsendmessage_click (object sender, System.EventArgs e)

{

Open queue

System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue (". \\Private$\\MSMQDemo");

Create message

System.Messaging.Message Message = new System.Messaging.Message ();

Message. Body = TxtMessage.Text.Trim ();

Message. Formatter = new System.Messaging.XmlMessageFormatter (new type[] {typeof (String)});

Put message into queue

Queue. Send (message);

}

Receive Message

private void Btnreceivemessage_click (object sender, System.EventArgs e)

{

Open queue

System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue (". \\Private$\\MSMQDemo");

Receive message, the synchronous receive method blocks the current thread of execution until a message can be

System.Messaging.Message Message = queue. Receive ();

Message. Formatter = new System.Messaging.XmlMessageFormatter (new type[] {typeof (String)});

Txtreceivemessage.text = message. Body.tostring ();

}

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.