Use MSMQ

zhaozj2021-02-11  173

MSMQ (Microsoft Message Queue, Microsoft Message Queue) is an asynchronous transmission mode that implements mutual communication between multiple different applications, and the application of mutual communication can be distributed on the same machine, or it can be distributed in connected network spaces. No location. Its implementation principle is that the sender of the message puts the information you want to send in a container (we call it Message), then save it to a system public space message queue (MSSAGE Queue); local or It is the message receiver that is off-site and then proceed from the queue to its message.

In the message delivery mechanism, there are two important concepts. One is a message, one is a queue. The message is information that is transferred by the two parties of communication, which can be a wide variety of media such as text, sound, images, and more. The final understanding of the message is agreed in advance, and the benefits of doing this are equivalent to simply encrypting the data, two uses their own defined format to save communication. The message can contain the identity of the send and recipient, so that only the specified user can see only the information passed to him and the return of whether or not to operate successfully. The message can also contain a timestamp to facilitate the processing of certain applications. The message can also contain the expiration time, which indicates that the message is notned in the specified time, which is made, mainly applied with a relatively close application.

The message queue is a public storage space that sends and receives messages, which can exist in memory or in physical files. The message can be sent in two ways, ie express mode (Express) and recoverable modes, the difference is that the express mode is placed in memory for the quick delivery of the message, not on the physical disk, To get a high processing ability; the recoverable mode is written to the physical disk in each step of the transfer process to obtain better fault recovery capabilities. The message queue can be placed on the machine where the sender is located, or it can be placed separately on another machine. It is because the flexibility of the message queue is in the placement mode, and the reliability of the message delivery mechanism is formed. When the machine is saved by the message queue, the message sent by the recoverable mode can be restored to the state before the failure, and the message sent in the express manner is lost. On the other hand, the message delivery mechanism is used. If the sender is necessary to worry about whether the recipient is started, whether a fault or the equal is not necessary, as long as the message is successfully sent, it can be considered to be completed, but in fact, the other party may even have no power, or It may be a second day when you actually complete the transaction.

The benefits of using MSMQ are: Since it is asynchronous communication, whether the sender or the receiver does not have to wait for the other party to return success messages, the remaining code can be improved, and thus greatly increase the ability of things; when the information transfer The information transmission mechanism has a function of fault recovery capabilities; the message transfer mechanism of MSMQ makes the two parties of the message communications have different physical platforms.

Using the MSMQ feature provided on Microsoft's .NET platform, you can easily create or delete messages, send or receive messages, or even manage the message queue.

In the .NET product, an MSMQ class library "System.Messaging.dll" is provided. It provides two classes to operate the message object and message queue object, respectively. Before you can use the MSMQ function, you must determine the MSMQ message queue component on your machine and make sure the service is running. When using ASP.NET programming, you should use it at the head:

<% @ AskEMBLY Name = "System.Messaging"%>

<% @ Import namespace = "system.message"%> introducing the MSMQ class library into the ASP.NET file

1. Create a message queue

DIM MSGQUE As MessageQueue

Msgque = new messageQueue (msgpath)

Among them: Msgpath can be a local private queue, such as "./myqueue", or a public queue of other machines, such as "Saidy / 777 $ / MyQueue", saidy is another machine name.

2. Message transmission

DIM MSGQUE As MessageQueue

Msgque.send (MSG)

Where: MSG is either.

3. Message reception

The reception of the message is divided into two synchronous and asynchronous methods. Synchronous receives the first message received from the message queue in the specified time. When there is no message in the message queue, the program is in the waiting state; the asynchronous reception method is defined An event handler that triggers the function immediately when the first message arrives in the message queue.

1) Synchronous mode

DIM MSG As Message

DIM FMT AS XMLMESSAGEFORMATTER

FMT = ctype (msgque.formatter, XmlMessageFormatter)

FMT.Targettypenames = new string () {"system.string"}

Msg = msgque.receive (New TimeSpan (0,0,3))

First define the format that receives the message should be converted, then receive the message within the specified time.

2) asynchronous mode

DIM FMT AS XMLMESSAGEFORMATTER

'Define the receiving message type

FMT = ctype (msgque.formatter, XmlMessageFormatter)

FMT.Targettypenames = new string () {"system.string"}

'Define Message Processing Functions

AddHandler Msgque.ReceiveCompleted, New ReceiveCompletedEventHandler

(AddressOf OnRecompleted)

'Define Message Processing Functions

Public Shared Sub OnreceiveCompleted (S as Object, AsyncResult As ReceiveasynceventArgs)

DIM msgque as messagequeue = ctype (s, messagequeue)

DIM msg as message = msgque.endreceive (asyncRESult.asyncResult)

'At this time, Msg.Body is the message object being taken.

Msgque.beginreceive ()

'Redefining asynchronous reception

End Sub

'Start asynchronous reception method

Msgque.beginReceive

Message queue configuration properties

About queue properties

Path Attribute: It can decide three ways, path reference, format name reference, and identification reference.

Category Properties: Identify the type of the currently used queue. Category is the GUID value defined by the queue owner. The GUID value can have a GUID generation tool to generate or use a user-defined numeric value. The GUID value will not be unique, so it can be divided into different categories according to the same GUID value. Category.

Follow the properties related to the send data type

Formatter Properties: Decide how to send and receive messages in a queue, and what kind of content can be sent in a message. And the queue translational attribute

Denysharereceive properties: Decide that only one component can access the message in the message queue in the same time.

CANREAD and CANWRITE properties: Determines if the queue can be read or written.

MaximumQueuesize and MaximumJournalsize Properties: Set the message for a queue (log queue) with a kilobyte to maximize the amount. Once the received message reaches this capacity, the new message will no longer be received.

In general, the maximum value of the message queue is set to the message queue administrator. If this value is not controlled, then the maximum capacity of the default message queue will be unlimited.

UseJournalQueue Properties :: Set whether you copied the message to the log message queue.

转载请注明原文地址:https://www.9cbs.com/read-5681.html

New Post(0)