MSMQ (Microsoft Message Queue) is the basis of message applications in Windows operating systems and a development tool for creating Distributed and loosely connected messaging applications. Message queues and emails have many similarities. They all contain multiple attributes for saving messages. The addresses of senders and receivers are indicated in message types; however, their usefulness is very different: the sender and receiver of a message queue are applications, while the sender and receiver of an email are usually people.
Like an email, the sender and receiver of A Message Queue do not need to be present at the same time. They can be stored in a message queue or an email server. Therefore, we can describe the architecture of the MSMQ application:
It can be seen that developing MSMQ applications is not very difficult. However, to use MSMQ to develop your message processing program, you must install message queue on the development system and host that uses the program. The installation of message queue is the installation of Windows components, which is similar to that of general components. After the MQ is installed, you can develop your own message processing program. However, if your computer is in a working group rather than a domain, your public queue may not be available, but this does not affect your program development.
The message processing program sends and receives messages. However, to send and receive messages, you must also reference a queue. Generally, we reference public queues and dedicated queues, both of which store user-generated messages. After the queue is referenced, you can send, receive, and read messages. The message receiving service is located in system. messaging. If you cannot find this namespace, you must manually add it. Click [add reference] in [project], click the Browse button, find the system. messaging. dll file, and add it.
Reference queue
There are three methods to reference a queue: using a path, a format name, and a tag to reference a queue. Here I will only introduce the simplest and most commonly used method: using a path to apply a queue. The queue path is machinename/queuename. The path to the queue is always unique. The following table lists the paths used for each type of queue:
Queue type
Syntax used in the path
Public queue
Machinename/queuename
Dedicated queue
Machinename/private $/queuename
Log queue
Machinename/queuename/Journal $
If it is sent to the local machine, you can also use "." To represent the local name. The specific reference method is carried out through the path attribute, or when the message queue is initialized.
If a message queue is referenced during initialization, the message queue must exist in the system; otherwise, an interruption occurs. It is very easy to add a queue to the system. Open [Computer Management] in [control panel], expand [services and applications], find and expand [Message Queue] (if not found, indicates that you have not installed a message queue.) Right-click the type of the Message Queue you want to add and select create queue. Of course, you can also create a message queue in the program. The following describes how to create a message queue. The code for referencing a Message Queue during initialization is very simple, as shown below:
Messagequeue MQ = new messagequeue (". // Private $ // Jiang ");
The code for referencing a Message Queue through the path attribute is also very simple:
Messagequeue MQ = new messagequeue ();
MQ. Path = ". // Private $ // Jiang ";
You can use the Create method to create a queue on a computer:
System. messaging. messagequeue. Create (@ "./private $/Jiang ");
Send message
After the queue is referenced, the message can be sent. Messages can be sent into simple messages and complex messages. simple messages are commonly used data types, such as integer and string data; the Data Types of complex messages usually correspond to complex data types in the system, such as structures and objects.
An example of sending a simple message is as follows:
MQ. Send (1000); // send Integer Data
MQ. Send ("This is a Test message !"); // Send a string
The transmission of complex messages is similar to that of simple messages. Only when a message is sent, the variable is used to send the message content instead of directly sending the message content. The following code sends a complex message through the message variable and complex data type variable respectively.
// The message sent in 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 following code is represented by a variable of the complex data type. Customer is a custom class.
Customer customer = new customer ();
Customer. lastname = "Copernicus ";
Customer. firstname = "niclaus ";
MQ. Send (customer );
Receive messages
Receiving messages is a little more complex than sending messages. Two methods are used to receive a message: the receive method is used to receive the message and permanently delete the message from the queue. The peek method is used to retrieve the message from the queue without removing the message from the queue. If you know the message identifier (ID), you can use the receivebyid method and the peekbyid method to complete the corresponding operation.
The message receiving code is simple:
MQ. Receive (); // or MQ. receivebyid (ID );
MQ. Peek (); // or MQ. peekbyid (ID );
Read messages
Only the messages received can be read out. Therefore, you must read the messages after receiving them. Reading the messages is the most complex operation. The messages that can be read by the application are in different formats from those in the message queue. Therefore, the messages sent by the application are serialized before being sent to the Message Queue. This process is automatically completed by the system, program developers do not have to write code for this, but they are faced with message serialization after receiving messages.
Message serialization can be performed through Visual Studio and. net Framework comes with three predefined formatters to complete: xmlmessageformatter object (default formatter setting of the messagequeue component), binarymessageformatter object, and activexmessageformatter object. Because messages formatted by the latter two cannot be read by humans, we often use xmlmessageformatter objects.
The following code uses the xmlmessageformatter object to format a message:
String [] types = {"system. String "};
(Xmlmessageformatter) MQ. formatter). targettypenames = types;
Message M = MQ. Receive (New timespan (0, 0, 3 ));
After the received message is sent to the Message variable, the message can be read through the body attribute of the message variable M:
MessageBox. Show (string) M. Body );
Disable Message Queue
Closing a message queue is simple. Like other objects, you can use the close function to achieve the following:
MQ. Close ();
So far, the basic knowledge of the MSMQ application has been fully introduced, but developing a powerful MSMQ application is obviously not that simple, for more details, refer to the Message Queue