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
- using System;
- using System.Collections.Generic;
- using System.Messaging;
- namespace Msmq
- {
- class Program
- {
- Private Const string Path = @". \private$\ljz";
- Private Static MessageQueue queue = messagequeue.exists (path)? New MessageQueue (path): messagequeue.create (Path, true);
- //private static list<string> List = new list<string> ();
- Public enum Level
- {
- Low,
- Normal,
- High
- }
- Static void Main (string[] args)
- {
- Send ("First time!", Level.low);
- Send ("second time!", Level.normal);
- Send ("The third time!", Level.high);
- Console.WriteLine (Receive ());
- Console.WriteLine (Receive ());
- Console.WriteLine (Receive ());
- Send ("Async for the first time!", Level.normal);
- Send ("Async second time!", Level.high);
- Send ("Asynchronous third time!", Level.low);
- Receivebyasyn ();
- //foreach (string str in list)
- //{
- //Console.WriteLine (str);
- //}
- Console.readkey ();
- }
- Public Static void Send (string content, level)
- {
- System.Messaging.Message Message = new System.Messaging.Message ();
- Message. Formatter = new System.Messaging.BinaryMessageFormatter ();
- Message. Body = content;
- Switch (level)
- {
- Case Level.low:
- Message. priority = Messagepriority.low;
- break;
- Case Level.high:
- Message. priority = Messagepriority.high;
- break;
- default:
- Message. priority = Messagepriority.normal;
- break;
- }
- //messagequeuetransaction tran = new MessageQueueTransaction ();
- //try
- //{
- //Tran. Begin ();
- //queue. Send (message);
- //Tran.commit ();
- //}
- //catch
- //{
- //Tran. Abort ();
- //}
- Queue. Send (message, messagequeuetransactiontype.automatic);
- }
- Public Static string Receive ()
- {
- System.Messaging.Message Message = queue. Receive ();
- Message. Formatter = new System.Messaging.BinaryMessageFormatter ();
- return message. Body.tostring ();
- }
- Public Static void Receivebyasyn ()
- {
- Queue. receivecompleted + = new receivecompletedeventhandler (queue_receivecompleted);
- Queue. BeginReceive ();
- }
- Private Static void queue_receivecompleted (object sender, Receivecompletedeventargs e)
- {
- MessageQueue queuetmp = Sender as MessageQueue;
- System.Messaging.Message mess = Queuetmp.endreceive (E.asyncresult);
- Mess. Formatter = new System.Messaging.BinaryMessageFormatter ();
- Console.WriteLine (mess. Body.tostring ());
- //list. ADD (mess. Body.tostring ());
- Queuetmp.beginreceive ();
- }
- }
- }
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
- Public: [Machinename]\[queuename]
- 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