Introduction to Message Queue and its usage (zz)

Source: Internet
Author: User
Tags msmq
Introduction and use of Message Queue

Monday, 20. March 2006, 02:57:30

Message Queue

With Microsoft Message Queue (MSMQ), application developers can easily communicate with applications quickly and reliably by sending and receiving messages. Message Processing provides you with reliable fault prevention methods that ensure message transmission and perform many business processes.

Like XML Web Services and. Net remoting, MSMQ is a distributed development technology. However, when using XML Web Services or. NET remoting components, the client needs to exchange information with the server in real time, and the server needs to stay online. MSMQ can work when the server is offline. The message is temporarily stored in the client message queue, and then sent to the server for processing when the server is online.

Obviously, MSMQ is not suitable for the case where the client needs a timely response from the server. MSMQ interacts with the server asynchronously without worrying about waiting for a long time to process the server.

Although both XML Web Services and. Net remoting provide the [oneway] attribute to process asynchronous calls, it is used to solve the problem of long method calls on the server side blocking the client side for a long time. However, it cannot solve the problem of a large number of client loads. In this case, the server accepts requests faster than processing requests.

Generally, the [oneway] attribute is not used in a dedicated message service.

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

A message is a data unit transmitted between two computers. Messages can be very simple, for example, containing only text strings, or more complex, and may contain embedded objects.

The message is sent to the queue. "Message Queue" is the container that stores messages during message transmission. The message queue manager acts as a man-in-the-middle when a message is relayed from its source to its target. The main purpose of a queue is to provide a route and ensure message transmission. If the receiver is unavailable when a message is sent, the Message Queue retains the message until it can be passed successfully.

"Message Queue" is Microsoft's message processing technology. It provides message processing and Message Queue functions for any application in any computer combination installed with Microsoft Windows, whether these computers are on the same network or online at the same time.

The message queue network is a group of computers that can send messages to and from each other. Different computers in the Network play different roles in ensuring smooth message processing. Some of them provide routing information to determine how to send messages, some store important information of the entire network, and some only send and receive messages.

During the "message queue" installation, the Administrator determines which servers can communicate with each other and sets special roles for specific servers. Computers that make up the message queue network are called sites, which are connected to each other through site links. Each site link has an associated "overhead", which is determined by the Administrator, indicating the frequency of message passing through the site link.

The "message queue" Administrator also sets one or more computers on the network as the "Routing Server. The routing server can view the overhead of links of different sites and determine the fastest and most effective way to transmit messages through multiple sites to determine how to transmit messages.

2. Queue type)

There are two main types of Queues: queues created by you or other users in the network and system queues.

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

The "Public queue" is replicated across the "message queue" network and may be accessed by all sites connected to the network.

"Dedicated queue" is not released across the network. Instead, they are only available on the local computer where they reside. A dedicated queue can only be accessed by applications that know the full path name or tag of the queue.

"Management queue" contains messages that confirm the receipt of messages sent in a given "message queue" network. Specifies the management Queue (if any) to be used by the messagequeue component ).

The "response queue" contains the response message returned to the sending application when the target application receives the message. Specify the response queue (if any) to be used by the messagequeue component ).

The queues generated by the system are generally divided into the following types:

The "Diary queue" can be used to store the copy of the sent message and the copy of the removed message from the queue. A single diary queue on each "message queue" client stores copies of messages sent from this computer. A separate diary queue is created for each queue on the server. This diary tracks messages removed from this queue.

"Dead-letter queue" stores copies of messages that cannot be passed or have expired. If an expired message or a message that cannot be delivered is a transactional message, it is stored in a special dead-letter queue, called a "transactional dead-letter queue ". Dead messages are stored on the computer where the expired messages are located. For more information about the timeout period and expiration message, see Default Message attributes.

The "Report queue" contains the message indicating that the message is routed to the target, and it can also contain the Test message. Each computer can have only one report queue.

"Dedicated system queue" is a dedicated queue for managing and notifying messages required by a series of storage systems to perform message processing operations.

Most of the work done in applications involves accessing public queues and their messages. However, several different system queues may be used in daily operations based on the application's logging, validation, and other special processing needs.

3. Synchronous and Asynchronous Communication (Synchronous VS. Asynchronous Communication)

Queue communication is inherently asynchronous, because sending messages to the queue and receiving messages from the queue are completed in different processes. In addition, the receiving operation can be performed asynchronously, because the recipient can call the BeginReceive Method for any given queue, and then immediately continue other tasks without waiting for a response. This is quite different from what people know about "synchronous communication.

In synchronous communication, the request sender must wait for a response from the specified receiver before executing other tasks. The sender's waiting time depends entirely on the time used by the receiver to process the request and send the response.

4. Interacting with Message Queues)

Message Processing and messaging provide a powerful and flexible mechanism for inter-process communication between server-based application components. Compared with direct calls between components, they have several advantages, including:

Stability-the impact of component failure on messages is much smaller than that of direct calls between components, because messages are stored in the queue and remain there until they are properly processed. Message Processing is similar to transaction processing because message processing is guaranteed.
Message priority-more urgent or important messages can be received before relatively unimportant messages, so sufficient response time can be guaranteed for key applications.
Offline capabilities-when messages are sent, they can be sent to the temporary queue and stay there until they are successfully transmitted. When the access to the required queue is unavailable for any reason, you can continue to perform the operation. At the same time, other operations can be continued, just as the message has been processed, because the message transmission is guaranteed when the network connection is restored.
Transactional message processing-coupling multiple related messages into a single transaction to ensure that messages are delivered in order and only once, and can be successfully retrieved from their target queue. If any error occurs, the entire transaction is canceled.
Security-The MessageQueue component uses Windows security to protect access control, provide audit, and encrypt and verify messages sent and received by the component.

5. Compile a simple Message Queue program in the. Net Environment

(1) install Message Queuing Services first

Use the Control Panel, "Add/Remove Programs"-"Add/Remove Windows Components" step to install MSMQ.

MSMQ can be installed in workgroup or domain mode. If the installer does not find a server that runs the Message Queue that provides the directory service, it can only be installed in Working Group mode, message queues on this computer only support creating dedicated queues and creating direct connections with other computers running message queues.

(2) Configure MSMQ

Open Computer Management-Message Queuing and create MSMQDemo queue under Private Queues

(3) write code-a simple demonstration of MSMQ objects

The MessageQueue class is the packaging around the message queue. The MessageQueue class provides reference to the "message queue" queue. You can specify a path to the existing resource in the MessageQueue constructor, or 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 synchronized Peek and Receive methods enable the process thread to wait for the new message to arrive at the queue at a specified interval. The asynchronous BeginPeek and BeginReceive methods allow the master application task to continue running in a separate thread before the message arrives in the queue. These methods work by using callback objects and State objects to facilitate information communication 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 btn1_emessage_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 execution thread until a message can get

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

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

TxtReceiveMessage. Text = message. Body. ToString ();

}

******

The introduction text about MSMQ Message Queue comes from MSDN.

References:

1. MSDN, Message Queue)

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.