Introduction to Message Queue and its use

巴扎黑
Release: 2016-12-20 09:25:03
Original
1425 people have browsed it

Using MSMQ (Microsoft Message Queue), application developers can easily communicate with applications quickly and reliably by sending and receiving messages. Message processing provides you with guaranteed message delivery and a reliable, fail-safe way to perform 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 with the Server in real time, and the Server needs to stay online. MSMQ can work when the server is offline, temporarily saving the message in the client's message queue, and then sending it to the server for processing when it is online later.

Obviously, MSMQ is not suitable for situations where the client requires a timely response from the server. MSMQ interacts with the server in an asynchronous manner, so there is no need to worry about waiting for the server to process for a long time.

Although XML Web Services and .Net Remoting both provide the [OneWay] attribute to handle asynchronous calls, which is used to solve the problem of long method calls on the server side that hinder the client side for a long time. However, it cannot solve the problem of a large number of Client loads. At this time, the Server accepts requests faster than it processes them.

Generally, the [OneWay] attribute is not used in specialized messaging services.

1. Basic terms and concepts

"Message" is a unit of data transmitted between two computers. Messages can be very simple, containing only text strings, or they can be more complex, possibly containing embedded objects.

The message is sent to the queue. A "message queue" is a container that saves messages during their transmission. The message queue manager acts as a middleman when relaying messages from its source to its destination. The main purpose of a queue is to provide routing and guarantee the delivery of messages; if the recipient is unavailable when the message is sent, the message queue holds the message until it can be successfully delivered.

"Message Queuing" is Microsoft's messaging technology that provides message processing and message queuing functionality to any application on any combination of computers with Microsoft Windows installed, regardless of whether the computers are on the same network or not. online at the same time.

A "message queue 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 processed 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 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," which is determined by the administrator and indicates how often messages are delivered through the site link.

The "Message Queuing" administrator also sets up one or more computers as "routing servers" in the network. The routing server determines how to deliver the message by looking at the cost of each site link to determine the fastest and most efficient way to deliver the message through multiple sites.

2. Queue Type

There are two main queue types: queues created by you or other users on the network and system queues.

User-created queues may be any of the following:

"Public queues" are replicated throughout the Message Queuing network and potentially accessible by all sites connected to the network.

"Private Queues" are not published across the entire network. Instead, they are only available on the local computer where they reside. Private queues can only be accessed by applications that know the queue's full pathname or label.

A “Management Queue” contains messages acknowledging receipts for messages sent in a given “Message Queue” network. Specifies the management queue (if any) you want the MessageQueue component to use.

The "response queue" contains the response messages returned to the sending application when the target application receives the message. Specifies the response queue (if any) you want the MessageQueue component to use.

System-generated queues are generally divided into the following categories:

"Journal Queue" optionally stores copies of messages sent and copies of messages removed from the queue. A single journal queue on each Message Queuing client stores copies of messages sent from that computer. A separate journal queue is created on the server for each queue. This journal tracks messages removed from this queue.

The "Dead Letter Queue" stores copies of messages that cannot be delivered or have 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." Dead letters are stored on the same computer as the expired messages. For more information about timeout periods and expired messages, see Default Message Properties.

The "report queue" contains messages indicating the route the message took to reach its destination, and can also contain test messages. There can be only one report queue per computer.

"Private system queues" are a series of dedicated queues that store management and notification messages required by the system to perform message processing operations.

Most of the work done in the application involves accessing public queues and their messages. However, depending on your application's logging, acknowledgment, and other special processing needs, you are likely to use several different system queues in day-to-day operations.

3. 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. Additionally, receive operations can be performed asynchronously, since the person who wants to receive the message can call the BeginReceive method on any given queue and then immediately continue with other tasks without waiting for a reply. This is very different from what people know as "synchronous communication."

In synchronous communication, the sender of the request must wait for a response from the intended recipient before performing other tasks. The time the sender waits depends entirely on the time it takes 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. They have several advantages over direct calls between components, including:

Stability - component failure affects the message much less than direct calls between components, because the message is stored in the queue and remains there, until properly handled. Message processing is similar to transaction processing in that message processing is guaranteed.

Message Priority – More urgent or important messages can be received before less important messages, thus ensuring adequate response time for critical applications.

Offline Capability - When messages are sent, they can be sent to a temporary queue and remain there until successfully delivered. Users can continue operations when access to the required queue is unavailable for any reason. Meanwhile, other operations can continue as if the message has been processed, because message delivery is guaranteed when the network connection is restored.

Transactional message processing — Couples multiple related messages into a single transaction, ensuring that messages are delivered in order, delivered only once, and can be successfully retrieved from their destination queues. If any errors occur, the entire transaction will be canceled.

Security — The message queuing technology on which the MessageQueue component is based uses Windows security to protect access control, provide auditing, and encrypt and authenticate messages sent and received by the component.

5. Write a simple Message Queue program in .Net environment

(1) Install Message Queuing Services first

Install MSMQ through Control Panel, "Add/Remove Programs" - "Add/Remove Windows Components" steps .

MSMQ can be installed in workgroup mode or domain mode. If the installer does not find a server running Message Queuing that provides directory services, you can only install in workgroup mode. Message Queuing on this computer only supports the creation of private queues and the creation of shared queues with other computers running Message Queuing. direct connection.

(2) Configure MSMQ

Open Computer Management – ​​Message Queuing, create MSMQDemo queue under Private Queues

(3) Write code - simple demonstration of MSMQ objects

The MessageQueue class is around the "Message Queue" Package. The MessageQueue class provides a reference to the Message Queue queue. You can specify a path to an existing resource in the MessageQueue constructor, or you can create a new queue on the server. Before calling Send, Peek, or Receive, a new instance of the MessageQueue class must be associated with an existing queue.

MessageQueue supports two types of message retrieval: synchronous and asynchronous. The synchronous Peek and Receive methods cause the process thread to wait for a specified interval for new messages to arrive on the queue. The asynchronous BeginPeek and BeginReceive methods allow the main application task to continue execution in a separate thread before the message arrives on 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)

{

                                                   ​Queue(".\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, 同步的Receive方法阻塞当前执行线程,直到一个message可以得到

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

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

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

}


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!