The following example uses MSMQ to send and receive objects of the person and Order Types and uses transaction ).
1. First define the person class and order class:
// Personnel class <br/> public class person <br/> {<br/> Public Person () <br/> {<br/> m_name = ""; <br/> m_age = 0; <br/>}</P> <p> Public Person (string name, uint16 age) <br/>{< br/> m_name = Name; <br/> m_age = age; <br/>}</P> <p> // name <br/> Public string name <br/>{< br/> get {return m_name ;} <br/> set {m_name = value ;} <br/>}</P> <p> // age <br/> Public uint16 age <br/>{< br/> get {return m_age ;} <br/> set {m_age = value ;}< br/>}</P> <p> private string m_name; <br/> private uint16 m_age; <br/>}< br/>
// Shopping list <br/> public class order <br/> {<br/> Public Order () <br/> {<br/> price = 0.0; <br/> Number = 0; <br/> time = datetime. now; <br/>}</P> <p> Public Order (double price, uint64 number, datetime time) <br/>{< br/> price = price; <br/> Number = number; <br/> time = time; <br/>}</P> <p> // item unit price <br/> double price; </P> <p> // item quantity <br/> uint64 number; </P> <p> // order time <br/> datetime time; <br/>}
2. The following functions are used to create a message queue:
// Function for creating a Message Queue <br/> static void createmessagequeue () <br/>{< br/> string mqname = @". /private $/testmq "; </P> <p> // if the message queue already exists, it is not created <br/> If (! Messagequeue. exists (mqname) <br/>{< br/> messagequeue. Create (mqname, true); <br/>}< br/>}
3. The function for sending and receiving messages is defined below:
// Message sending function <br/> static bool send (person, order, string description) <br/>{< br/> messagequeue MQ = new messagequeue (@". /private $/testmq "); </P> <p> // specify that the message is encoded in XML format <br/> MQ. formatter = new xmlmessageformatter (New Type [] {typeof (person), typeof (Order), typeof (string )}); </P> <p> // define the transaction <br/> messagequeuetransaction transaction = new messagequeuetransaction (); </P> <p> try <br/> {<br/> // if the message queue uses a transaction, start the transaction <br/> If (MQ. transactional) <br/> transaction. begin (); </P> <p> message msg1 = new message (person); <br/> MQ. send (msg1, transaction); </P> <p> message msg2 = new message (order); <br/> MQ. send (msg2, transaction); </P> <p> message msg3 = new message (description); <br/> MQ. send (msg3, transaction); </P> <p> // If a Message Queue uses a transaction, stop the transaction. <br/> If (MQ. transactional) <br/> transaction. commit (); </P> <p> return true; <br/>}< br/> catch (exception ex) <br/>{< br/> // if the message queue adopts a transaction and an exception occurs, terminate the transaction. <br/> If (MQ. transactional) <br/> transaction. abort (); </P> <p> return false; <br/>}< br/>}
// Function for receiving messages <br/> static object [] receive () <br/>{< br/> messagequeue MQ = new messagequeue (@". /private $/testmq "); </P> <p> // defines all types of messages in the Message Queue (the order between types can be exchanged) <br/> type [] msgtypes = new type [] {typeof (person), typeof (Order), typeof (string )}; </P> <p> // specify that the message is encoded in XML format <br/> MQ. formatter = new xmlmessageformatter (msgtypes); </P> <p> // defines the transaction <br/> messagequeuetransaction transaction = new messagequeuetransaction (); </P> <p> try <br/> {<br/> // if the message queue uses a transaction, start the transaction <br/> If (MQ. transactional) <br/> transaction. begin (); </P> <p> message msg1 = MQ. receive (transaction); <br/> person = (person) msg1.body; </P> <p> message msg2 = MQ. receive (transaction); <br/> order = (Order) msg2.body; </P> <p> message msg3 = MQ. receive (transaction); <br/> string description = (string) msg3.body; </P> <p> // if the message queue uses transactions, stop the transaction <br/> If (MQ. transactional) <br/> transaction. commit (); </P> <p> return new object [] {person, order, description };< br/>}< br/> catch (exception ex) <br/>{< br/> // if the message queue adopts a transaction and an exception occurs, terminate the transaction. <br/> If (MQ. transactional) <br/> transaction. abort (); </P> <p> return NULL; <br/>}< br/>}
4. The following two main functions are used to send and receive messages respectively. (Generally, the sender and receiver of a message are not in the same process or in the same thread .)
// Main function of the message sender <br/> static void main (string [] ARGs) <br/>{< br/> // create a message queue <br/> createmessagequeue (); </P> <p> person = new person ("Jackie ", 30); <br/> order = New Order (110.0, 10, datetime. now); <br/> string description = "this is a new order. "; </P> <p> If (send (person, order, description) <br/>{< br/> console. writeline ("message sent successfully! "); <Br/>}< br/> else <br/>{< br/> console. writeline (" message sending failed! "); <Br/>}< br/>}
// Message Receiver main function <br/> static void main (string [] ARGs) <br/>{< br/> // create a message queue <br/> createmessagequeue (); </P> <p> // receives messages. If the returned value is null, the receiving fails. <br/> object [] Message = receive (); </P> <p> If (message! = NULL) <br/>{< br/> // when the message is successfully received, the message is retrieved and saved to the following objects in sequence <br/> person = (person) message [0]; <br/> order prder = (Order) Message [1]; <br/> string description = (string) Message [2]; </P> <p> console. writeline ("Message received successfully! "); <Br/>}< br/> else <br/>{< br/> console. writeline (" message receipt failed! "); <Br/>}< br/>}