Use of Message Queue MSMQ and Message Queue MSMQ
1. Install MSMQ
Control Panel-programs and functions-enable or disable Windows functions-Microsoft Message Queue (MSMQ) server, select all, and click OK.
2. Application Scenario of Message Queue (Reprinted from http://www.cnblogs.com/stopfalling/p/5375492.html)
① Asynchronous Processing
② Application decoupling
③ Traffic charge Cutting
④ Log processing
⑤ Message Communication
3. MSMQ messages are classified into transactional and non-transactional messages.
Non-transactional messages are stored in the memory, and messages in the message queue are lost after the machine restarts.
Transactional messages are persisted and saved on the hard disk. After the machine is restarted, messages in the queue will not be lost.
4. Test code
Message sender
Namespace MessageQueueSender {class Program {static void Main (string [] args) {MessageQueue MSMQ = CreateMessageQueue (@". \ private $ \ tpmsmq "); MSMQ. formatter = new XmlMessageFormatter (new Type [] {typeof (string)}); Console. writeLine ("Continue sending message: Y/N? "); String cmd = Console. readLine (); while (cmd. equals ("Y") {Sender (MSMQ); Console. writeLine ("Continue sending message: Y/N? "); Cmd = Console. readLine ();} Console. writeLine ("press any key to stop... "); Console. readKey ();} private static void Sender (MessageQueue MSMQ) {try {string random = GenerateRandom (); string obj = string. format ("{0} Sender: {1}", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss"), random); MSMQ. send (obj, MessageQueueTransactionType. single); Console. writeLine (obj);} catch (Exception ex) {Console. writeLine (string. format ("{0} Sender: {1}", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss"), ex. message) ;}} public static MessageQueue CreateMessageQueue (string path) {MessageQueue mq = null; if (MessageQueue. exists (path) {mq = new MessageQueue (path);} else {mq = MessageQueue. create (path, true) ;}return mq;} public static string GenerateRandom () {int seed = GetRandomSeed (); return new Random (seed ). next (Int32.MaxValue ). toString ();} /// <summary> // create an encrypted random number generator to generate a strongly Random Seed // </summary> /// <returns> </returns> private static int GetRandomSeed () {byte [] bytes = new byte [4]; System. security. cryptography. RNGCryptoServiceProvider rng = new System. security. cryptography. RNGCryptoServiceProvider (); rng. getBytes (bytes); return BitConverter. toInt32 (bytes, 0 );}}}
Message recipient
Namespace MessageQueueReceiver {class Program {static void Main (string [] args) {MessageQueue MSMQ = CreateMessageQueue (@". \ private $ \ tpmsmq "); MSMQ. formatter = new XmlMessageFormatter (new Type [] {typeof (string)}); Receiver (MSMQ);} private static void Receiver (MessageQueue MSMQ) {while (true) {try {Message m = MSMQ. receive (MessageQueueTransactionType. single); Console. writeLine (string. format ("{0} receiver: [{1}]", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss"), m. body. toString ();} catch (Exception ex) {Console. writeLine (string. format ("{0} receiver: {1}", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss"), ex. message) ;}} public static MessageQueue CreateMessageQueue (string path) {MessageQueue mq = null; if (MessageQueue. exists (path) {mq = new MessageQueue (path);} else {mq = MessageQueue. create (path, true) ;}return mq ;}}}
The MessageQueue. Receive () method is executed synchronously. If there is no message in the queue, blocking will occur, and the program will stop waiting for the message to be generated.