C#Queue Learning Notes: Introduction to MSMQ 1

1. Introduction

MSMQ, the full name of MicroSoft Message Queue, Microsoft Message Queue, is an asynchronous transmission mode for communication between different applications. Applications that communicate with each other can be distributed on the same machine or anywhere in the connected network space.It works by putting the message the sender wants to send into a container (we call it Message), then saving it to a message queue in a system public space, from which local or remote message receivers can retrieve messages for processing.

A Message message is the information that both sides of the communication need to transmit.

The types of queues include the following:

Public Queues replicate across Message Queuing networks and may be accessed by all sites connected to the network.

Private queues are not published across the network; instead, they are available only on the local computer where they reside.Private queues can only be accessed by applications that know the full path name or label of the queue.

Manage Queues contains messages confirming receipts for messages sent in a given Message Queuing network.Specify the administrative queue (if any) that you want the MessageQueue component to use.

The Response Queue contains the response message returned to the sending application when the target application receives the message.Specify the response queue (if any) that you want the MessageQueue component to use.

Advantages: stability, message priority, offline capability, and security, reliable fail-safe mechanisms for guaranteed message delivery and execution of many business processes.

Disadvantages: MSMQ is not suitable for clients requiring real-time Server-side interaction; response delays when large requests occur.

2. Installation

Enter appwiz.cpl->Turn Windows features on or off->Microsoft Message Queue (MSMQ) server at run time.

 

After successful installation, enter compmgmt.msc->services and applications->message queue at run time.

3. Demonstration of examples

    class Program
    {
        static void Main(string[] args)
        {
            #region MSMQ Introduction One
            //Create a message queue with default storage path: C:\WINDOWS\system32\msmq\storage
            if (MessageQueue.Exists(@".\Private$\HelloMSMQ"))
                MessageQueue.Delete(@".\Private$\HelloMSMQ");   //Delete message queue HelloMSMQ
            if (MessageQueue.Exists(@".\Private$\WorldMSMQ"))
                MessageQueue.Delete(@".\Private$\WorldMSMQ");   //Delete message queue WorldMSMQ

            MessageQueue mqHello = MessageQueue.Create(@".\Private$\HelloMSMQ");
            MessageQueue mqWorld = MessageQueue.Create(@".\Private$\WorldMSMQ");

            //send message
            mqHello.Send("Hi World,I am Hello.", "mqHello1");
            mqHello.Send("Are you free?", "mqHello2");
            mqHello.Send("Hi Hello,I am World.", "mqWorld1");
            mqHello.Send("I guess I'll be free.", "mqWorld2");

            //Return messages for all private queues on this machine
            foreach (MessageQueue item in MessageQueue.GetPrivateQueuesByMachine("cx168"))
            {
                item.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
                Message[] messages = item.GetAllMessages();
                foreach (Message message in messages)
                {
                    Console.WriteLine($"Label: {message.Label} Body: {message.Body}");
                }
            }

            //Returns the message for the specified queue
            if (MessageQueue.Exists(@".\Private$\HelloMSMQ"))
            {
                using (MessageQueue theOne = new MessageQueue(@".\Private$\HelloMSMQ"))
                {
                    Console.WriteLine();

                    //Set Message Queue Formatter
                    theOne.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
                    //Accept but do not delete messages
                    Message msg = mqHello.Peek();
                    Console.WriteLine($"Label: {msg.Label} Body: {msg.Body}");
                    //Accept and delete messages
                    msg = mqHello.Receive();
                    Console.WriteLine($"Label: {msg.Label} Body: {msg.Body}");
                    msg = mqHello.Peek();
                    Console.WriteLine($"Label: {msg.Label} Body: {msg.Body}");
                    //Delete all messages
                    mqHello.Purge();
                    Console.WriteLine($"mqHello count: {mqHello.GetAllMessages().Count()}");
                }
            }

            Console.Read();
            #endregion
        }
    }

The results are as follows:

 

Reference from:

    https://www.cnblogs.com/tenghoo/archive/2009/11/05/1596456.html

Tags: C# network Windows

Posted on Sun, 22 Mar 2020 22:04:14 -0700 by Myke