PetShop 4.0 Learning Note: Message Queuing MSMQ

Source: Internet
Author: User
Tags msmq

It was not until today that the window system we used every day had such a good programming component: Message Queuing. It can solve the performance problem in the case of large data exchange, especially the database performance of BS system. And its asynchronous processing can give programmers the greatest convenience and the best user experience.

1. First install MSMQ on the server that requires Message Queuing, my system is WIN2003+IIS6, so this installation option is within the application server, add the Remove Programs->windows component. Default is not installed, you need to manually select.

2. Establish a storage path for Message Queuing. This can be added within Windows Computer Management or in a program. I was adding it in the program.

3. To refer to system.messaging in. NET;

C # code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Messaging;
  4. namespace Msmq
  5. {
  6. class Program
  7. {
  8. Private Const string Path = @". \private$\ljz";
  9. Private Static MessageQueue queue = messagequeue.exists (path)? New MessageQueue (path): messagequeue.create (Path, true);
  10. //private static list<string> List = new list<string> ();
  11. Public enum Level
  12. {
  13. Low,
  14. Normal,
  15. High
  16. }
  17. Static void Main (string[] args)
  18. {
  19. Send ("First time!", Level.low);
  20. Send ("second time!", Level.normal);
  21. Send ("The third time!", Level.high);
  22. Console.WriteLine (Receive ());
  23. Console.WriteLine (Receive ());
  24. Console.WriteLine (Receive ());
  25. Send ("Async for the first time!", Level.normal);
  26. Send ("Async second time!", Level.high);
  27. Send ("Asynchronous third time!", Level.low);
  28. Receivebyasyn ();
  29. //foreach (string str in list)
  30. //{ 
  31. //Console.WriteLine (str);
  32. //} 
  33. Console.readkey ();
  34. }
  35. Public Static void Send (string content, level)
  36. {
  37. System.Messaging.Message Message = new System.Messaging.Message ();
  38. Message. Formatter = new System.Messaging.BinaryMessageFormatter ();
  39. Message. Body = content;
  40. Switch (level)
  41. {
  42. Case Level.low:
  43. Message. priority = Messagepriority.low;
  44. break;
  45. Case Level.high:
  46. Message. priority = Messagepriority.high;
  47. break;
  48. default:
  49. Message. priority = Messagepriority.normal;
  50. break;
  51. }
  52. //messagequeuetransaction tran = new MessageQueueTransaction ();
  53. //try
  54. //{ 
  55. //Tran. Begin ();
  56. //queue. Send (message);
  57. //Tran.commit ();
  58. //} 
  59. //catch
  60. //{ 
  61. //Tran. Abort ();
  62. //} 
  63. Queue. Send (message, messagequeuetransactiontype.automatic);
  64. }
  65. Public Static string Receive ()
  66. {
  67. System.Messaging.Message Message = queue. Receive ();
  68. Message. Formatter = new System.Messaging.BinaryMessageFormatter ();
  69. return message. Body.tostring ();
  70. }
  71. Public Static void Receivebyasyn ()
  72. {
  73. Queue. receivecompleted + = new receivecompletedeventhandler (queue_receivecompleted);
  74. Queue. BeginReceive ();
  75. }
  76. Private Static void queue_receivecompleted (object sender, Receivecompletedeventargs e)
  77. {
  78. MessageQueue queuetmp = Sender as MessageQueue;
  79. System.Messaging.Message mess = Queuetmp.endreceive (E.asyncresult);
  80. Mess. Formatter = new System.Messaging.BinaryMessageFormatter ();
  81. Console.WriteLine (mess. Body.tostring ());
  82. //list. ADD (mess. Body.tostring ());
  83. Queuetmp.beginreceive ();
  84. }
  85. }
  86. }

Here are some explanations for the above code:

1. The above code does not add exception handling, in fact, like this type of code is bound to add exception handling!

2. Path. There are several types of Message Queuing, some of which can be accessed by others, such as public queues, and others can only be accessed on their own, such as private queues. Please refer to the reference articles listed below. But not every one of them can be used. Computers like my workgroup-mode computer can use only dedicated queues, That you can only access yourself. There are differences in the way they are written:

C # code
    1. Public: [Machinename]\[queuename]
    2. Private: [Machinename]\private$\[queuename]

3. There are two important classes for using Message Queuing: System.Messaging.Message and System.Messaging.MessageQueue. I list the full name because there is a class with the same name in. Net. Don't confuse it. The following MessageQueue and message refer specifically to the above two classes.

MessageQueue is a Message Queuing object with two important methods: send and receive. As the name implies, send is a message to Message Queuing, and receive receives messages from the message queue.

The message is a messaging object. It has two important properties, formatter is its content serialization format attribute, and body is the body of its contents. In the above example, I put a string, in fact, it can also put objects, if the object must be able to be serialized.

According to the above mentioned content, has been able to write a message queue demo, but not enough practical, the following is to say that some of its practical properties.

1. Priority. Admittedly, in the actual application of the message is a priority, some messages need priority to be processed by the message is general. How to embody it? The message object has the Priority property, which has 8 levels, and I used only three of them in the example above. By setting priorities, you can enable important messages to be processed first.

2. Transactions. Sometimes, in the process of transmitting data, it is necessary to ensure the consistency of the data, in the database often use transactions, in fact, there are transactions, in the above example, 56-66 lines of code is transaction processing, but the more cumbersome to write, in fact, you can use 67 lines of writing, let the system itself to maintain.

3. Asynchronous processing. The MessageQueue object's receive method has the disadvantage that when there is no data in the queue, the line Cheng dead until there is new data coming in. This is actually not good. A better approach is to open a new thread to deal with, This will not affect existing threads. 80 and 81 lines in. NET, which are written as examples, let the message object load a delegate, and then place the processing code inside the delegate.

The 11th line of code, 32-35 lines, 90 lines is my intention to note off, in fact, I think that when the asynchronous processing of the message, what should be done is to take the data out of it, and should not be processed inside. Instead, the data is processed elsewhere. I declare a local variable list, Supposedly, the 90th row of data should be able to join the list, but the fact is that the data taken out can sometimes be added, sometimes not. I think this should be a multi-threaded problem. NET disables inter-threading between threads. List is a variable of the main thread, which is not allowed to be assigned to a child thread after it has been opened asynchronously. But what I can't figure out is why sometimes it's possible to be successful. Maybe I understand wrong. If any of the cows saw my confusion, I hope to show my twos. Now I don't know much about multithreading. (Sweat one ~ ~ ~)

In fact, personally, the scope of application of Message Queuing is still relatively limited, first of all, he can only be the window system, in. NET down, and then, it has higher network bandwidth requirements. So in the LAN application is relatively good, in the Interent application, in fact, there is a better solution:. Net Remoting and Web Services

Demo Download:

http://ljzforever.qupan.com/?folder=951925

Reference articles:

Asp. NET for message processing (MSMQ)

Http://dev.yesky.com/178/8196178.shtml

Asp. NET for message processing (MSMQ) two

Http://dev.yesky.com/229/8196229.shtml

. net+msmq quick access to the database

Http://developer.ccidnet.com/art/322/20030214/37984_1.html

Introduction to Message Queuing and its use

Http://www.cnblogs.com/rickie/archive/2004/11/16/64345.html

Message transmission and Reception test summary between MSMQ two domains

Http://www.cnblogs.com/billqi/archive/2005/12/29/307371.html

MSMQ application issues in ASP. Thank you!

Http://topic.csdn.net/t/20020829/11/979610.html

. Net Environment application of Message Queuing (MSMQ) objects

Http://www.cnblogs.com/rickie/archive/2004/11/17/64712.aspx

. NET MSMQ Asynchronous calls

Http://www.wangchao.net.cn/bbsdetail_37204.html

Using MSMQ

Http://myxq.cnblogs.com/archive/2005/03/15/119150.aspx

PetShop 4.0 Learning Note: Message Queuing MSMQ

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.