Introduction
MSMQ (Microsoft Message Queue) is the basis of the message application in the Windows operating system, which is a development tool for creating a distributed, loose connection message communication application. Message queues and emails have a lot of similarities, they all contain multiple properties, used to save messages, all pointed out the address of the sender and recipient; however, their use has a big difference: message queue transmission People and recipients are applications, while email senders and recipients are usually people.
Like email, the send and reception of message queues do not require the sender and the recipient to be in the same field, and can be stored in a message queue or a mail server. Therefore, we can use the following figure to describe the architecture of the MSMQ application:
As can be seen from the above figure, developing MSMQ applications is not very difficult. However, use MSMQ to develop your message handler, you must install the message queue on the development system and using the program. The installation of the message queue belongs to the installation of the Windows component, and the general component installation method is similar. After installing the message queue, you can develop your own message handler. However, it is important to pay attention to it. If your computer is in a working group, not a domain, it may not be used by your public queue, but this does not affect your program development.
Message handler is nothing more than messaging, but to send and receive messages, you must also reference a queue, usually we reference public queues and dedicated queues, both queues store users generated messages. After reference to the queue, you can send, receive, and read messages. The message receiving service is in System.Messaging if you can't find this namespace, you must manually add it. Click [Add Reference] in [Project], press the Browse button, find the system.Messaging.dll file, add it.
Quotation
The queue has three methods, through the path, format name, and label queue, here I only introduce the easiest and most common methods: through the path application queue. The form of the queue path is Machnename / Queuename. The path pointing to the queue is always unique. The following table lists the path information for each type of queue:
Queue type
Syntax used in the path
Public queue
Machinename / queuename
Special queue
Machinename / private $ / queuename
Log queue
Machinename / Queuename / Journal $
If it is sent to this unit, you can also use "." Represents the native name. The specific reference method is carried out by the PATH attribute, or it can be performed when the message queue is initialized.
If a message queue is referenced when initialization, the message queue must exist in the system, otherwise the interrupt will be generated. Add a queue to the system, open [Computer Management] in [Control Panel], open [Service and Application], to find and expand [Message Queue] (If you can't find it, you haven't found a message queue yet. , Right-click the category of the message queue you want to add, select the new queue ready. Of course, the creation of the message queue can also be implemented in the program, which will have the corresponding description below. The code queue queue queue is very simple when initialization, as shown below:
MessageQueue MQ = New MessageQueue (".//prate $ // jiang");
The code that references the message queue through the Path property is also very simple:
MessageQueue MQ = New MessageQueue ();
MQ.Path = ".//prate $ // jiang";
Use the CREATE method to create queues on your computer:
System.Messaging.MessageQueue.create (@ "./ private $ / jiang"); send message
After the queue references, you can send a message. Messages can be divided into simple messages and complex messages, simple message types are commonly used data types, such as integer, string, etc., data types of complex messages usually correspond to complex data types in the system, such as structures, objects, etc. Wait.
The send example of simple messages is as follows:
Mq.send (1000); // Send integer data
Mq.send ("this is a test message!"); // Send a string
The transmission of complex messages and simple messages are similar, just when sending, usually not directly given message content, but represents a variable that sends a message content. The following code sends a complex message by message variables and complex data type variables, respectively.
// The message sent by the following code is represented by the message variable
Message msg;
Msg = New Message ("A Complex Message!");
Msg.label = "this is the label";
Msg.priority = messagepriority.high;
Mq.send (MSG);
The message sent in the code below is represented by a complex data type variable, Customer is a custom class.
Customer Customer = New Customer ();
Customer.lastname = "COpernicus";
Customer.firstname = "nicolaus";
Mq.send (Customer);
Receive a message
The received message is complicated than a message. The received message is permanently deleted from the queue from the queue from the queue from the queue through the Receive method; remove the message from the queue from the queue through the PEEK method without removing the message from the queue. If you know the identifier (ID) of the message, you can also complete the corresponding operation via the ReceiveByID method and the Peekbyid method.
The code for receiving the message is simple:
Mq.Receive (); // or mq.receivebyid (id);
Mq.peek (); // or mq.peekbyid (id);
Read message
The received message is only available to read out, so the message must be read after receiving the message, and the read message is the most complex operation. Different in the message format in the message and message queue you can read, so the message sent out is sent to the message queue after serialization. This process is automatically completed by the system. The program developers don't have to write this. Code, however, after receiving the message, it faces a message sequence.
The serialization of the message can be completed through the three predefined formatters included with Visual Studio and .NET Framework: The XMLMessageFormatter object (the default formatting program settings for MessageQueue components), BinaryMessageFormatter objects, ActiveXMessageFormatter objects. Since the formats of the latter two usually cannot read, we often use the XMLMESSAGEFORMATTER object.
The code to format the message using the XMLMessageFormatter object is as follows:
String [] Types = {"system.string"};
(XMLMESSAGEFORMATTER) mq.formatter) .targettypenames = Types
Message m = mq.receive (New TimeSpan (0,0,3));
After transmitting the received message to the message variable, you can read the message by the Body attribute of the message variable m: messagebox.show (String) m.body);
Close message queue
The closure of the message queue is simple, like other objects, can be implemented through the close function:
Mq.close ();
So far, the basic knowledge of the MSMQ application is fully, but developing a powerful MSMQ application is obviously not as simple. To learn more more information, please refer to the help content of the message queue in the MSDN and Windows operating systems. .