C # cohort study notes: MSMQ Getting Started

    I. Introduction

    MSMQ Cyberspace Full MicroSoft Message Queue, Microsoft Message Queue, is to realize an ATM intercommunication between a plurality of different applications, applications communicate with each other may be distributed on the same machine, it may be connected to the distribution of any position. Its implementation principle is: the sender of the message you want to send the information into a container (we call Message), and then save it to a message queue (Message Queue) a system of public space, local or message receiving program places the message sent to it and then removed from the queue for processing.

    Message message information by the communication of the parties need to pass.

    Queue type include the following:

    "Public queues" copy the whole "message queue" the network, and may be accessed by all sites Fi.

    "Private queues" the entire network is not released, on the contrary, they are available only on the local computer resides. Private queues can only be accessed by the application program to know the full path name or label of the queue.

    "Queue Management" contains an acknowledgment of receipt message in a given "message queue" message sent by the network. Specify that you want to use the MessageQueue component management queue (if any).

    Sent back to the application in response to the message "response queue" contains the target application receives a message. Designating a desired response queue MessageQueue component used (if any).

    Pros: stable, message priority, offline capabilities and security, guaranteed message delivery and implementation of reliable fail-safe mechanism of many business processes.

    Drawback: MSMQ is not suitable for real-time interaction situations Server Client needs to end; when a large number of requests response delay.

    Second, the installation

    Enter appwiz.cpl-> Turn Windows features on the fly -> Microsoft Message Queue (MSMQ) server.

 

    After successful installation, input compmgmt.msc-> services and applications running -> Message Queue.

    Third, the example shows

    class Program 
    { 
        static  void Main ( String [] args) 
        { 
            #region MSMQ Getting a
             // create a message queue, the default storage path: C: \ WINDOWS \ system32 \ MSMQ \ Storage 
            IF (MessageQueue.Exists ( @ " . \ Private $ \ HelloMSMQ " )) 
                MessageQueue.Delete ( @" \ Private $ \ HelloMSMQ. " );    // delete the message queue HelloMSMQ 
            IF (MessageQueue.Exists ( @" \ Private $ \ WorldMSMQ. " )) 
                MessageQueue.Delete ( @" . \ Private $ \ WorldMSMQ ");   //删除消息队列WorldMSMQ

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

            //发送消息
            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");

            //返回本机所有私有队列的消息
            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}");
                }
            }

            //返回指定队列的消息
            if (MessageQueue.Exists(@".\Private$\HelloMSMQ"))
            {
                using (MessageQueue theOne = new MessageQueue(@".\Private$\HelloMSMQ"))
                {
                    Console.WriteLine();

                    //Set the Message Queue formatter 
                    theOne.Formatter = new new the XmlMessageFormatter ( new new  String [] { " System.String The " });
                     // receiving the message but does not delete 
                    the Message MSG = mqHello.Peek (); 
                    Console.WriteLine ($ " the Label: Body msg.Label} {:} {msg.Body " );
                     // receive and delete a message 
                    MSG = mqHello.Receive (); 
                    Console.WriteLine ($ " the Label: msg.Label {Body}: {} msg.Body " ); 
                    MSG = mqHello.Peek();
                    Console.WriteLine($"Label: {msg.Label} Body: {msg.Body}");
                    //删除所有消息
                    mqHello.Purge();
                    Console.WriteLine($"mqHello count: {mqHello.GetAllMessages().Count()}");
                }
            }

            Console.Read();
            #endregion
        }
    }

    Results are as follows:

 

    Reference from:

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

Guess you like

Origin www.cnblogs.com/atomy/p/12549598.html
Recommended