MSMQ Message Queuing

Source: Internet
Author: User
Tags msmq

First, Introduction

Windows Communication Foundation (WCF) is the unified programming model that Microsoft provides for building service-oriented applications that provide serialization capabilities that support loose coupling and versioning, and provide Message Queuing (MSMQ), Microsoft's existing distributed system technologies, such as COM +, ASP. NET Web services,. Remoting, etc. The WCF platform makes it easy for developers to build service-oriented applications (SOA). It can be argued that WCF is the integration and extension of previously existing distributed technologies (that is, technologies such as MSMQ,. NET remoting, and Web services), and in that case it is necessary to first understand the previous distributed technologies, in order to understand more profoundly the benefits of WCF. Share this distributed technology of MSMQ today.

Ii. introduction of MSMQ

The MSMQ full name is Microsoft message queue--Microsoft Messages queue. It is an asynchronous transfer mode that enables communication between different applications, which can be distributed on the same machine or in any location in the connected network space.

2.1 How MSMQ Works

The implementation of MSMQ is that the sender of the message puts the information he wants to send into a container, and then saves it to a message queue in a system's public space, and the local or offsite message receiver processes the message sent to it from that queue.

Message Queuing is a public storage space that can exist in memory or in physical files, so messages are sent in two ways, express mode and recoverable mode. The difference is that the message store location, Express way, in order to fast message delivery, so put the message in memory, rather than on the physical disk, in order to obtain high processing power, and the recoverable mode in each step of the transfer process, the message is written to the physical disk, This way, when the machine that holds the message queue fails and restarts, the sent message can be restored to the state before the fault is sent for better message recovery. Message Queuing can be placed on the same machine as the sender, receiver, or separately on another machine. In addition, using the Message Queuing mechanism, the sender does not have to worry about whether the receiver is started or failed, as long as the message is sent successfully, it can be considered that the processing is complete, and the other party may not even boot, or the actual message delivered to the other party may be the next day. The MSMQ mechanism is similar to the QQ message delivery mechanism. Demonstrates the implementation principle of MSMQ.

There are two main concepts in MSMQ.

    • One is the message message:message is the message that both sides of the communication need to pass, it can be text, picture, video and so on. The message contains the identity of the sender and recipient, and only the specified user can obtain the message.
    • One is the queue of queues: the storage space used to hold messages, and the main types of queues in MSMQ are:
      • Public queues: replicated throughout the Message Queuing network, potentially accessible to all sites connected by the network. The path format is: machine name \ queue Name
      • Private queues (or Private Queues): They are not published throughout the network, they are available only on the local computer where they reside, and private queues can only be accessed by applications that know the full path name or label of the queue. The path format is: Machine name \private$\ queue name
      • Journal queue: contains acknowledgment of message receipt messages sent in a given message queue. The path format is: machine name \ queue name \journal$
      • Response queue: Contains the response message that is returned to the sending application when the target application receives the message, including machine log queues, machine dead-letter queues, and machine transaction dead-letter queues.
        • The machine log queue corresponds to the format: machine name \journal$;
        • The machine dead-letter queue corresponds to the format: machine name \deadletter$;
        • The machine channel dead-letter queue corresponds to the format: machine name \xactdeadletter$.

2.2 Description of the queue reference

When a MessageQueue instance is created, it should indicate which queue to communicate with, and there are 3 ways to access the specified message queue in. NET:

    • With a path, the path to the message queue is uniquely determined by the machine name and queue name, so you can use the message queue path to indicate the message queue you are using.
    • Using the format name, which is the unique identity generated by MSMQ when the message queue was created, a mission is not specified by the user, but is automatically generated by the Queue Manager GUID.
    • Uses the identity name (label), which is a meaningful name that is specified by the message manager when Message Queuing is created.

Iii. advantages and disadvantages of Message Queuing

The advantage of using Message Queuing is that because it is asynchronous communication, both the sender and the receiver will be able to execute the rest of the code without waiting for the other party to return a successful message, which greatly improves the ability of processing, and has the capability of failure recovery during information transmission. MSMQ's messaging mechanism makes it possible for both sides of the communication to have different physical platforms.

The disadvantage of Message Queuing is that it is not suitable for client to need server side real-time interaction, and the response may be delayed when a large number of requests.

Iv. development of distributed applications using MSMQ 4.1 environment preparation

Want to be in. NET platform for the development of MSMQ, need to install Message Queuing, you need to open Control Panel----open or close Windows features, tick the Message Queuing service all options, as shown in the following:

After you click OK after checking, you can see the following figure in Message Queuing, services and applications, my computer, management--

Seeing this diagram above indicates that you have successfully configured MSMQ's development environment, you can use visual Studio to develop it. Note that you must successfully install the corresponding queue type for the operation code for a particular type of queue.

4.2 Developing distributed applications using MSMQ

First, implement the server side. Create a console project and add a system.messaging reference, because the class for Message Queuing is all encapsulated in the System.Messaging.dll assembly. The code for the specific server is as follows:

 1 using System; 2 using System.Messaging;             3 4 Namespace Msmqserver 5 {6 class program 7 {8 static void Main (string[] args) 9 {10 Create a public queue that can only be created in a domain environment//if (!  Messagequeue.exists (@ ". \learninghardmsmq"))//Determine if the queue is already available under this path//{13//using (MessageQueue MQ = Messagequeue.create (@ ". \learninghardmsmq"))//{+//MQ. Label = "Learninghardqueue"; Set Queue label ("a public queue has been created"), +//Console.WriteLine ("Path: {0}", M Q.path//Console.WriteLine ("queue name: {0}", MQ.) QueueName); +//MQ. Send ("MSMQ Message", "leaning hard"); Send Message//}21//}22//if (messagequeue.exists (@ ". \private$\learninghardmsmq") ) 24//{25//Delete message queue://Messagequeue.delete (@ ". \private$\learninghardmsmq");     27//}28        Create a private message queue with the IF (! Messagequeue.exists (@ ". \private$\learninghardmsmq")) (MessageQueue MQ = Messageque Ue. Create (@ ". \private$\learninghardmsmq")). Label = "Learninghardprivatequeue"; Console.WriteLine ("A private queue has been created"); Console.WriteLine ("Path: {0}", MQ.) Path); Console.WriteLine ("Private queue name: {0}", MQ.) QueueName), PNs MQ. Send ("MSMQ Private Message", "leaning hard"); Send Message 38}39}40 41//Traverse all public message Queues//foreach (MessageQueue MQ in Me Ssagequeue.getpublicqueues ())//{44//MQ. Send ("Sending MSMQ public message" + DateTime.Now.ToLongDateString (), "Learning hard");//Console.writel INE ("Public Message was sent to {0}", MQ. Path),//}47 if (messagequeue.exists (@ ". \private$\learninghArdmsmq ")) 49 {50//Get private message queue MessageQueue MQ = new MessageQueue (@". \private $\learninghardmsmq "); Send ("Sending MSMQ private message" + DateTime.Now.ToLongDateString (), "leaning hard"); Console.writelin E ("Private Message is sent to {0}", MQ. Path);}55 Console.read (); 57}58}59}

Server-side code note that public queues can only be created in a domain environment, and because my PC is not joined to a domain environment, it is not possible to create a public queue, as can be seen from the beginning of the message queue, and no public queues are installed in the diagram.

After implementing the server side, it is natural to complete the client. The main principle of the MSMQ program is that the server sends the message to the shared message queue, and then the client takes the message out of the shared message queue for processing. The implementation code for the specific client is as follows:

 1 using System; 2 using System.Messaging; Need to add System.Messaging Reference 3 4 namespace Msmqclient 5 {6 class program 7 {8 static void Main (string[] ar GS) 9 {messagequeue.exists (@ ". \private$\learninghardmsmq")) 11 {12/                 /create a Message Queuing object using the MessageQueue MQ = new MessageQueue (@ ". \private$\learninghardmsmq")) 14 {15//Set the formatter for Message Queuing to MQ. Formatter = new XMLMessageFormatter (new string[] {"System.String"}), and the Message msg in MQ. GetAllMessages ()) {Console.WriteLine ("Received Private Message is: {0}", Msg. Body);}21 Message firstmsg = MQ. Receive (); Gets the first message in the message queue, Console.WriteLine ("Received, Private message is: {0}", firstmsg.       Body);}25}26 Console.read (); 27  }28}29} 
4.3 Running the demo

After the above steps, we have completed the implementation of the MSMQ distributed program, see how to run the program to see the effect.

First, it is natural to start the server, right-Msmqserver project, debugging, start a new instance to start the server, as shown in the following steps:

After running successfully, you will send the message to the server successfully to the console interface as follows:

Next, run the client to get the message from the message queue and display it in the console, starting the client in the same way as the server, right-msmqclient-> debugging, starting a new instance, and seeing the client's effect as shown:

As you can see, the client did successfully get the message in the message queue.

The above MSMQ program requires special attention: messagequeue.receive () is to take out the first message in the queue in the message queue and remove it from the Message Queue (MSDN Chinese translation is an error, MSDN writes is not removed, and the English text is removed), The actual result is also removed, if you run the client again, you will find that there is only one message in the message queue, the effect is as follows:

V. Summary

Here, the content of MSMQ to share the end, its implementation of the principle of MSMQ is very simple, one word is that the server put the message in a public place, this place is called the message queue, and other clients can remove the message from this place to process. The next chapter will share another distributed technology on the. NET platform ——. NET Remoting.

MSMQ Message Queuing

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.