. Net uses message queues, and uses windows components to store a series of tasks to be completed. Without using the same queue as a program, it facilitates data sharing and collaboration between different programs ......
In my own experience, this is similar to session in some aspects (of course there are many differences). In the same way, session can store information in the aspnet_state service, recompile the website, or restart the website, the session will not be lost (the session timeout is normal, except in this case ).
Install the Message Queue component in win7. For other operating systems, search for relevant information.
If the service is not automatically started, you need to start the service:
Create a queue first, and then use the queue. one message in the queue is sent, and one message is received.
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Messaging;
// Add a reference to the physical file System. Messaging
Namespace testweb
{
Public partial class MSMQtest: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// CreateNewQueue ("MsgQueue"); // create a message queue
// SendSimpleMsg (); // It is recommended that each queue only send and receive information in the same format. Otherwise, it is difficult to convert the format.
// ReceiveSimpleMsg ();//
// ReceiveSimpleMsg ();
// SendComplexMsg ();
// ReceiveComplexMsg ();
MsgModel m = receiveComplexMsg <MsgModel> ();
Response. Write (m. ToString ());
}
Private void sendSimpleMsg ()
{
// Instantiate MessageQueue and point to an existing queue named VideoQueue
MessageQueue MQ = new MessageQueue (@ ". \ private $ \ MsgQueue ");
// MQ. Send ("message test", "Test message ");
System. Messaging. Message message = new System. Messaging. Message ();
Message. Label = "message lable ";
Message. Body = "message body ";
MQ. Send (message );
Response. Write ("message sent successfully," + DateTime. Now + "<br/> ");
}
Private void receiveSimpleMsg ()
{
MessageQueue MQ = new MessageQueue (@ ". \ private $ \ MsgQueue ");
// Call the Receive method of MessageQueue to Receive messages
If (MQ. GetAllMessages (). Length> 0)
{
System. Messaging. Message message = MQ. Receive (TimeSpan. FromSeconds (5 ));
If (message! = Null)
{
// Message. Formatter = new System. Messaging. XmlMessageFormatter (new string [] {"Message. Bussiness. VideoPath, Message"}); // message type conversion
Message. Formatter = new System. Messaging. XmlMessageFormatter (new Type [] {typeof (string )});
Response. write (string. format ("message received successfully, lable: {0}, body: {1}, {2} <br/>", message. label, message. body. toString (), DateTime. now ));
}
}
Else
{
Response. Write ("no message! <Br/> ");
}
}
Private void sendComplexMsg ()
{
// Instantiate MessageQueue and point to an existing queue named VideoQueue
MessageQueue MQ = new MessageQueue (@ ". \ private $ \ MsgQueue ");
// MQ. Send ("message test", "Test message ");
System. Messaging. Message message = new System. Messaging. Message ();
Message. Label = "lable complex message ";
Message. Body = new MsgModel ("1", "message 1 ");
MQ. Send (message );
Response. Write ("message sent successfully," + DateTime. Now + "<br/> ");
}
Private void receiveComplexMsg ()
{
MessageQueue MQ = new MessageQueue (@ ". \ private $ \ MsgQueue ");
// Call the Receive method of MessageQueue to Receive messages
If (MQ. GetAllMessages (). Length> 0)
{
System. Messaging. Message message = MQ. Receive (TimeSpan. FromSeconds (5 ));
If (message! = Null)
{
Message. Formatter = new System. Messaging. XmlMessageFormatter (new Type [] {typeof (MsgModel)}); // message Type conversion
MsgModel msg = (MsgModel) message. Body;
Response. write (string. format ("message received successfully, lable: {0}, body: {1}, {2} <br/>", message. label, msg, DateTime. now ));
}
}
Else
{
Response. Write ("no message! <Br/> ");
}
}
Private T receiveComplexMsg <T> ()
{
MessageQueue MQ = new MessageQueue (@ ". \ private $ \ MsgQueue ");
// Call the Receive method of MessageQueue to Receive messages
If (MQ. GetAllMessages (). Length> 0)
{
System. Messaging. Message message = MQ. Receive (TimeSpan. FromSeconds (5 ));
If (message! = Null)
{
Message. Formatter = new System. Messaging. XmlMessageFormatter (new Type [] {typeof (T)}); // message Type conversion
T msg = (T) message. Body;
Return msg;
}
}
Return default (T );
}
/// <Summary>
/// Create a message queue
/// </Summary>
/// <Param name = "name"> Message Queue name </param>
/// <Returns> </returns>
Public void CreateNewQueue (string name)
{
If (! System. Messaging. MessageQueue. Exists (". \ private $ \" + name) // check whether a Message Queue with the same name already Exists
{
System. Messaging. MessageQueue mq = System. Messaging. MessageQueue. Create (". \ private $ \" + name );
Mq. Label = "private $ \" + name;
Response. Write ("created successfully! <Br/> ");
}
Else
{
// System. Messaging. MessageQueue. Delete (". \ private $ \" + name); // Delete A Message Queue
Response. Write ("already exists <br/> ");
}
}
}
[Serializable]
Public class MsgModel
{
Public string id {get; set ;}
Public string Name {get; set ;}
Public MsgModel (){}
Public MsgModel (string _ id, string _ Name)
{
Id = _ id;
Name = _ Name;
}
Public override string ToString ()
{
If (string. IsNullOrEmpty (id) | string. IsNullOrEmpty (Name) return "";
Return string. Format ("id -- {0}, Name -- {1}", id, Name );
}
}
}