Use of Message Queue MSMQ and Message Queue MSMQ

Source: Internet
Author: User
Tags random seed msmq

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.