See: http://hi.baidu.com/21tian/blog/item/ce5464097ddf10cb3ac76335.html
Why Message Queue
You may think that you can use a simple database table (an applicationProgramWrite data to it, and another application reads data from it) to apply the message queue. Message Queue platforms are more stable because they generally have their own security mechanisms, transaction support, and other functions. The routing function for message transmission is a key application. MSMQ provides various Message Queue platforms.
MSMQIntroduction
MSMQ is a component of Windows 2000, Windows XP, and Windows Server 2003 and will be included in Windows Vista and later Windows servers. Even if the target receiving application is not running, or the computer that runs the sending or receiving application is not connected to the Internet, collaborative applications can still use MSMQ to send and receive messages between each other. Before reaching the target queue, MSMQ stores and forwards messages. The receiving application can recover data from the queue.
MSMQ and other message queues feature separation of sending and receiving applications so that they do not have to run at the same time. This means that an application can store data in the queue, regardless of whether the project in the queue is transmitted to the receiving application.
MSMQ is an optional component of windows. You only need to add or delete Windows components through the Windows Control Panel Wizard to install MSMQ. MSMQ has two configuration modes: domain mode or workgroup mode (only private queue is used ). After MSMQ is installed, it can be run in the. NET application immediately.
MSMQInteraction
The development of message-based applications starts from the queue. MSMQ contains four queue types:
- Outbound queue: Use a message to temporarily store the message before it is sent to the destination.
- Public queue: Published in the Active Directory. Applications on various servers in the network can locate and apply public queues through active directories.
- Private queue: These are queues on the local server and are invalid for other servers (so these queues are not published in the Active Directory .)
- System queue: Includes the diary Queue (generated by the system), dead queue, and transaction-type dead message queue. Dead messages cannot be sent.
The system. messaging namespace executes the MSMQ programming operation. This namespace has two main objects:
-
- Message: The actual message or data sent or read by the queue.
-
- Messagequeue: MSMQ message queue for receiving/sending messages.
-
MSMQ programming
creating a queue is the first step to apply MSMQ. You can use the Message Queue option in the Windows computer management console to complete this operation, or you can program to create a queue by yourself. In list A, C # Code creates a new private MSMQ Message Queue (if no queue exists) and creates a message at the same time.
the Code uses the exists method of the messagequeue class to determine whether a private queue named techrepublic exists. If yes, it uses the messagequeue object of the existing queue example; otherwise, a new queue is created.
the new message object is used to send a message to the queue. Its label attribute specifies the Message Title displayed on the MSMQ console. Its subject contains the project content stored in the queue. In this case, I only send text, but you can use any type of object. The send method of the messagequeue class sends messages to the queue. List B contains the corresponding VB. NET code.
the next step is to read messages from the queue. This is a simple process. Just use the receive method of the messagequeue class. If a message exists in the queue, the receive method returns a message object; otherwise, it waits for a message to appear (you can set a time period ). To restore an object from a queue, you must know its type in advance.
The formatter attribute of the messagequeue class allows you to easily specify the type of the recovered object. The following simple example only uses text, so it applies system. String. In list C, the C # Code reads messages from the test queue.
the timespan object submitted to the receive method specifies the system wait time when an exception occurs. Next, set the formatter method in this example. The object is converted into a string to read the text stored previously. The receive method reads a message from the queue. Its value is displayed on the console. In the last part of the try block, the queue is disabled.
easily apply messages
MSMQ combines windows and. net System. the messaging namespace allows you to conveniently. NET application to use messages. Message provides a powerful tool for sending and receiving messages (data) asynchronously in enterprise applications.
Try the followingArticleSource
Http://hi.baidu.com/sanlng/blog/item/5fc5bb510d4faa2043a75b28.html
3. Send Information
1. Send simple messages
Before sending a message, you must instantiate an instance of messagequeue and specify the corresponding queue. The following code
// Instantiate messagequeue and point to an existing queue named videoqueue
Messagequeue MQ = new messagequeue (@ ". \ private $ \ videoqueue ");
MQ. Send ("message test", "Test message ");
(Code snippet 1)
2. Send complex messages
Videopath is the type to be transmitted as the boby of a message. Its definition is as follows:
Using system;
Namespace message. Bussiness
{
[Serializable]
Public class videopath
{
String _ sourcefilepath = string. empty;
String _ targetfilepath = string. empty;
Public videopath ()
{
}
Public videopath (string sourcefilepath, string targetfilepath)
{
This. _ sourcefilepath = sourcefilepath;
This. _ targetfilepath = targetfilepath;
}
Public String sourcefilepath
{
Get
{
Return this. _ sourcefilepath;
}
Set
{
This. _ sourcefilepath = value;
}
}
Public String targetfilepath
{
Get
{
Return this. _ targetfilepath;
}
Set
{
This. _ targetfilepath = value;
}
}
}
}
(Code snippet 2)
The requirement for this class is that the class must have a public constructor with no default parameters, all attributes must be readable and writable, and the class must be serializable. Use message to send messages to the queue.
Messagequeue MQ = new messagequeue (@ ". \ private $ \ videoqueue ");
Videopath vpath = new videopath (path. combine (server. mappath (". ")," video \ output. WMV "), path. combine (server. mappath (". ")," FLV \ output. FLV "));
System. messaging. Message message = new system. messaging. Message ();
Message. Label = "message used for video conversion ";
Message. Body = vpath;
MQ. Send (Message );
(Code snippet 3)
4. receive messages
1. Receive simple messages
2. Receive complex types of messages
ForThe message shown in code snippet 3 can be received as follows:
Messagequeue MQ = new messagequeue (@ ". \ private $ \ videoqueue ");
// Call the receive method of messagequeue to receive messages
System. messaging. Message message = MQ.Receive(Timespan. fromseconds (5 ));
If (message! = NULL)
{
Message. formatter = new system. messaging. xmlmessageformatter (New String [] {"message. bussiness. videopath, message "});
Videopath vpath = (videopath) message. Body;
Response. Write (vpath. sourcefilepath );
}
Else
{
Response. Write ("no message found! ");
}
Note that if a message is a custom object, you must specify the message serializer, that is, the formatter attribute. Improper formatter cannot deserialize messages correctly.