C # Use message queue to asynchronously process data storage

Source: Internet
Author: User
Message Queue is Microsoft's message processing technology. It provides message processing and Message Queue functions for any application in any computer combination installed with Microsoft Windows, whether these computers are on the same network or online at the same time.
The message queue network is a group of computers that can send messages to and from each other. Different computers in the Network play different roles in ensuring smooth message processing. Some of them provide routing information to determine how to send messages, some store important information of the entire network, and some only send and receive messages.
During the "message queue" installation, the Administrator determines which servers can communicate with each other and sets special roles for specific servers. Computers that make up the message queue network are called sites, which are connected to each other through site links. Each site link has an associated "overhead", which is determined by the Administrator, indicating the frequency of message passing through the site link.
Message Queue can be used to send and store data asynchronously. When you insert data to the database frequently, you can use message queue to insert data to the database asynchronously. Send the data to be inserted to the specified message queue. In the background, the data is read and inserted into the database. In PET shop4.0, the order receiving operation is to use the message queue, insert the next order into the message queue and then return immediately. The background receives the message and inserts the order.
The following two applets are used to write messages to the message queue and read messages from the Message Queue respectively. The interface is as follows:


I implemented it in vs2005. First, add the reference of system. messaging. dll:

Main Code:

// 1 create a message queue

Private void btncreate_click (Object sender, eventargs E)
{
String Path = txtpath. text;
Try
{
This. cursor = cursors. waitcursor;
// Determine whether the queue exists. If the queue does not exist, it is created.
Ensurequeueexists (PATH );
Myqueue = new system. messaging. messagequeue (path, true );

This. listview1.items. Clear ();
Tssl. Text = "created successfully! ";

}
Catch (messagequeueexception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
This. cursor = cursors. default;
}
}
Ensurequeueexists Method

/** // Check the queue. If the queue does not exist, create
///
/// Queue name
Private Static void ensurequeueexists (string path)
{
If (! System. messaging. messagequeue. exists (PATH ))
{
If (! System. messaging. messagequeue. exists (PATH ))
{
System. messaging. messagequeue. Create (path, false );
System. messaging. messagequeue mqtemp = new system. messaging. messagequeue (PATH );
Mqtemp. setpermissions ("everyone", system. messaging. messagequeueaccessrights. fullcontrol );
// Mqtemp. Label = "mmm ";
}
}
}
Send message
// Send a message
Private void btnsend_click (Object sender, eventargs E)
{
Messagequeuetransaction mytransaction = new messagequeuetransaction ();;
Try
{
If (myqueue = NULL)
{
Return;
}
// Order is a Class Object with two objects: Message and sending time.
Model. Order order = new model. Order ();
Order. Message = txtmessage. text;
Order. sendtime = datetime. now;

// Add a transaction, provided that the Message Queue supports transactions during creation

Mytransaction. Begin ();
// Send
Myqueue. Send (order );
Mytransaction. Commit ();

Insertlistview (order );
}
Catch (messagequeueexception ex)
{
// Mytransaction. Abort ();
MessageBox. Show (ex. Message );
}

} Receiver:
Private void btnlisten_click (Object sender, eventargs E)
{
Try
{
String Path = txtpath. Text. Trim ();
If (! System. messaging. messagequeue. exists (PATH ))
{
MessageBox. Show ("this message queue does not exist ");
Txtpath. selectall ();
Return;
}
This. listview1.items. Clear ();

System. messaging. messagequeue myqueue = new system. messaging. messagequeue (PATH );
// This sentence is very important. serialize the received message data. Otherwise, I do not know the received data. The data type I sent is order, so this is also
Myqueue. formatter = new xmlmessageformatter (New Type [] {typeof (model. Order )});
Myqueue. receivecompleted + = new receivecompletedeventhandler (myreceivecompleted );
Myqueue. beginreceive ();
}
Catch (messagequeueexception ex)
{
MessageBox. Show (ex. Message );
}
}
Receive messages Asynchronously

Private void myreceivecompleted (Object source, receivecompletedeventargs asyncresult)
{
Try
{
Messagequeue MQ = (messagequeue) source;
System. messaging. Message MS = MQ. endreceive (asyncresult. asyncresult );
Listview1.invoke (New eventhandler (event), new object [] {Ms. Body, eventargs. Empty });


MQ. beginreceive ();
}
Catch (messagequeueexception ex)
{
MessageBox. Show (ex. Message );
}
Return;
}
Private void event (Object o, eventargs E)
{
Insertlistview (model. Order) O );
}
Private void insertlistview (model. Order OBJ)
{
Listviewitem item = new listviewitem ();
Item. Text = obj. message;
Item. subitems. Add (obj. sendtime. tostring ());
This. listview1.items. insert (0, item );
}
}

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.