Introduction to Message Queuing and its use

Source: Internet
Author: User
Tags message queue web services msmq
Introduction to Message Queuing and its useCopy from Rickie Lee ' s blog,thanks.
Message Queuing ( Message Queue ) Introduction and its use

With MSMQ (Microsoft message Queue), application developers can easily and quickly and reliably communicate with applications by sending and receiving messages. Message processing provides you with secure messaging and reliable fail-safe methods for performing 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 side needs to exchange information in real time with the server side, and the server needs to stay online. MSMQ can work with the server offline, temporarily saving messages to client-side message queues, and then sending them to server-side processing later online.

Obviously, MSMQ is not suitable for a situation in which the client requires a timely response from the server side, and MSMQ interacts with the server side in an asynchronous manner, without worrying about waiting for the long process on the server side.

Although both XML Web services and. Net remoting provide a [OneWay] property to handle asynchronous calls, the server-side long method calls are used to block the client side for a long time. However, the problem of a large number of client loads 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. Fundamental Terminology and concepts (basicTerms 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 a container in which messages are saved during the transmission of messages. The Message Queue Manager acts as a middleman 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 the message, and if the receiver is unavailable when the message is sent, Message Queuing retains the message until it can be passed successfully.

Message Queuing is a Microsoft messaging technology that provides message processing and Message Queuing for any application in a combination of computers with Microsoft Windows, regardless of whether they are on the same network or are online at the same time.

Message Queuing networks are any set of computers that can send messages back and forth to one another. Different computers in the network play different roles in ensuring that messages are handled smoothly. Some of them provide routing information to determine how messages are sent, some hold important information about the entire network, and some simply send and receive messages.

During Message Queuing Setup, administrators determine which servers can communicate with each other and set special roles for specific servers. The computers that make up this Message Queuing network are called sites and are connected to each other through site links. Each site link has an associated "cost", which is determined by the administrator, indicating how often the message is passed through this site link.

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

2. queue type

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

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

Public queues are replicated throughout the Message Queuing network and may be accessed by all sites that are connected to 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 pathname or label of the queue.

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

Response queues contain response messages that are returned to the sending application when a 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:

Journal queues Optionally store copies of messages that are sent and those that are removed from the queue. A single journal queue on each Message Queuing client stores a copy of messages sent from that computer. A separate journal queue is created for each queue on the server. This journal tracks messages that are removed from the queue.

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

Report queues contain messages that indicate the route that a message passes through to the destination, and you can include test messages. Only one report queue can be on each computer.

The private system queue is a series of private queues for the management and notification messages required by the storage system to perform message processing operations.

Most of the work done in an application involves accessing public queues and their messages. However, depending on the application's journaling, confirmations, and other special processing needs, several different system queues are likely to be used in day-to-day operations.

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

Queue communication is inherently asynchronous, because sending messages to and receiving messages from a queue is done in a different process. Alternatively, 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 proceed to other tasks immediately without waiting for a reply. This is very different from what people know about "synchronous communication".

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

4. interacting with Message Queuing (interacting with messagesqueues)

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

Stability-component failures affect messages much less than direct calls between components, because messages are stored in queues and remain there until they are properly processed. Message processing is similar to transaction processing because message handling is guaranteed.

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

Offline capabilities-When sending messages, they can be sent to a temporary queue and remain there until they are successfully delivered. When access to the required queues 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 had been processed, because the message delivery is guaranteed when the network connection is restored.

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

The security-messagequeue component is based on the Message Queuing technology that uses Windows security to secure access control, provide auditing, and encrypt and authenticate messages sent and received by components.

5. Write a simple message Queue program in the . Net Environment

(1) First install message Queuing Services

Through the control Panel, the Add/remove Programs-add/remove Windows Components step installs MSMQ.

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

(2) Configure MSMQ

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

(3) Writing code-simply demonstrating the MSMQ object

The MessageQueue class is the wrapper around Message Queuing. The MessageQueue class provides a reference to a 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 you call 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 cause the process thread to wait for the new message to arrive in the queue at 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 at the queue. These methods work by using callback objects and state objects to communicate information 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$//msmqde

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.