What is MSMQ?

Source: Internet
Author: User
Tags msmq
MSMQ (Microsoft Message Queue, Microsoft Message Queue) is an asynchronous transmission mode for communication between multiple different applications. Applications that communicate with each other can be distributed on the same machine, it can also be distributed anywhere in the connected network space. Its implementation principle is: the message sender puts the information he wants to send into a container (we call it message ), then, it is saved to the message queue in a public space of the system. local or remote message receiving programs then extract the messages sent to it from the queue for processing.

There are two important concepts in the message transmission mechanism. One is a message, and the other is a queue. A message is the information that must be transmitted by both parties. It can be a variety of media, such as text, sound, and image. The final way to understand a message is agreed by both parties. The advantage of this is that data is encrypted simply, second, you can use your own defined format to save the amount of communication transferred. A message can contain the sender and receiver IDs, so that only the specified user can see the information that is passed to him and the receipt that is returned for successful operation. A message can also contain a timestamp, so that the receiver can process certain time-related applications. A message can also contain an expiration time. It indicates that a message is voided if it has not arrived within the specified time, which is mainly used for time-based applications.

A message queue is a public storage space for sending and receiving messages. It can exist in memory or physical files. Messages can be sent in two ways, express and recoverable. The difference between express and recoverable modes is that the express delivery mode enables fast delivery of messages, messages are stored in the memory instead of on the physical disk to obtain high processing capability. The recoverable mode is in each step of the transfer process, all messages are written to the physical disk for better fault recovery. Message queues can be placed on the sender and receiver, or separately on another machine. It is precisely because of the flexibility of message queues in the placement mode that the reliability of the message transmission mechanism is formed. When the machine that stores the Message Queue fails and restarts, messages sent in recoverable mode can be restored to the status before the fault occurs, and messages sent by express delivery are lost. On the other hand, using the message transmission mechanism, the sender must worry about whether the receiver is started, whether a fault occurs, and other non-essential factors. As long as the message is successfully sent, the processing can be considered complete, in fact, the other party may not even start the instance, or the next day may have actually completed the transaction.

The advantage of MSMQ is that, due to asynchronous communication, the sender and receiver can execute the remaining code without waiting for the other party to return a successful message, therefore, it greatly improves the processing capability of things. In the process of information transmission, the information transmission mechanism has a certain function of fault recovery; MSMQ's message transmission mechanism makes it possible for both parties to communicate with each other to have different physical platforms.

Using the MSMQ function provided by Microsoft's. NET platform, you can easily create or delete message queues, send or receive messages, and even manage message queues.

In. Net, a MSMQ class library "system. messaging. dll" is provided ". It provides two classes to operate message objects and Message Queue objects respectively. Before using the MSMQ function, make sure that the MSMQ Message Queue component is installed on your machine and that the service is running. When using ASP. NET programming, you should use it in the header:

<% @ Assembly name = "system. messaging" %>

<% @ Import namespace = "system. messsaging" %>

Introduce the MSMQ class library into the ASP. NET File

1. Create a Message Queue

Dim msgque as messagequeue

Msgque = new messagequeue (msgpath)

Msgpath can be a local private queue, for example ". \ myqueue "can also be a public queue of other machines, such as" Saidy \ 777 $ \ myqueue ", and Saidy is the name of another machine.

2. Message sending

Dim msgque as messagequeue

Msgque. Send (MSG)

Where: MSG is any object.

3. receive messages

Message receiving is divided into two types: synchronous and asynchronous. synchronous receiving extracts the first message received from the Message Queue within the specified time. When there is no message in the message queue, the program is in the waiting state. The asynchronous receiving method defines an event processing function, which is triggered immediately when the first message in the message queue arrives.

1) synchronization 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 in which the received message should be converted, and then receive the message within the specified time.

2) asynchronous mode

Dim FMT as xmlmessageformatter
''Defines the type of the received message
FMt = ctype (msgque. formatter, xmlmessageformatter)
FMT. targettypenames = new string () {"system. String "}

''Defines the entry to the Message Processing Function
Addhandler msgque. receivecompleted, new receivecompletedeventhandler
(Addressof onreceivecompleted)

''Defines the Message Processing Function
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 retrieved message object.
Msgque. beginreceive ()
''Redefine the asynchronous Receiving Method
End sub

''Start asynchronous Receiving Method
Msgque. beginreceive

Message Queue configuration attributes

Queue attributes

Path attribute: it can be used to reference a queue in three ways: path reference, format name reference, and Id reference.

CATEGORY attribute: identifies the type of the currently used queue. Category is the guid value defined by the queue owner. The GUID value can be generated by a guid generation tool or a user-defined numeric value. The GUID value is not unique, so that multiple message queues can be divided into different categories based on the same guid value ).

Attributes related to the data sending type

Formatter attribute: determines the order in which messages are sent and received in a queue, and what content can be sent in a message.

Attributes related to queue Interaction

Denysharereceive attribute: only one component can access messages in the message queue at a time.

Canread and canwrite attributes: determines whether the queue can be read or written.

Maximumqueuesize and maximumjournalsize attributes: set the maximum message capacity of a queue (log queue) in kilobytes. Once the received message reaches this capacity, new messages will no longer be received.

Generally, the maximum value of the message queue is set by the Message Queue administrator. If this value is not controlled, the maximum capacity of the default message queue is unlimited.

Usejournalqueue attribute: sets whether to copy received messages to the log 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.