Message Queue Basic Usage instructions

Source: Internet
Author: User

First, install message Queue:

Before Win7, Control Panel, add the Delete component (Windows Message Queue).

WIN7~WIN8: Control Panel, programs and features, enabling or turning off Windows features (find Windows Message Queue Server) option, together with all subclasses, can be installed automatically.

Second, use the message Queue:

1) for communication between various types of servers and computers:

Locally, send yourself to yourself (directly the private name of the. \\Private$\\Queue).

Remote computer:

formatname:D irect=tcp:121.0.0.1\\private$\\queue Name
formatname:D irect=os:machinename\\private$\\queue name (only for computers that have been remotely added to the domain).
formatname:D irect=http://222.10.xx.xx/msmq/private$/queue Name

Note: The FormatName must be written in exactly the red notation! "Direct" can be arbitrary.

2) By default, the queue receives all of the information for that group. Sometimes we need to cross the domain but specify that a computer receives certain information (for example, N-units have a ... N-numbered computers are connected to a server queue, but different computers have unique numbers, and the information I send is sent only to specific computer numbers. Then you can use the label of System.message (which must refer to the class library before use), and the Code is as follows (including deleting the message after it is received to prevent more and more things in the queue):

This case is run on my own computer, the interface on the "Send" button and 2 text boxes (accept the message label 1 and 2, respectively), to prevent the time to demonstrate too long, using the asynchronous mechanism.

usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.messaging;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingMSG =System.Messaging.Message;namespacemessagequeuedemo{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private Asynctask<string> GetMessage (stringlabelid) {List<Msg> msgs =NewList<msg>(); //listen to the private queue on your current computerMessageQueue Msgqueue =NewMessageQueue (@". \private$\myqueue"); stringresult =NULL; Random R=NewRandom (); awaitTask.run (() =            {                //Use this method to allow you to dynamically delete a queue                varme =Msgqueue.getmessageenumerator2 (); MSG tmpmsg=NULL;  for(IEnumerator pointer =me; pointer.                MoveNext ();) {                    //returns the current queue firstTmpmsg =(MSG) pointer.                    Current; //if the label matches the ID that is acceptable to the current text box, delete the MSG                    if(Tmpmsg.label = =labelid) {tmpmsg=me.                        Removecurrent (); //analog receive time is differentThread.Sleep (R.next ( -, -)); //You must specify the storage information format for each MSGTmpmsg.formatter =NewXMLMessageFormatter (NewType[] {typeof(string) }); Msgs.                        ADD (TMPMSG); //must be added, since deleting the current MSG does not use "reset", which causes MoveNext to be false. me.                    Reset (); }                }                if(Msgs. Count >0) {result=string. Join (",", ( fromMinchmsgsSelectm.body.tostring ()));            }            }); returnresult; }        Private Async voidButton1_Click (Objectsender, EventArgs e) {            //initialize a queue that has already been createdMessageQueue Msgqueue =NewMessageQueue (@". \private$\myqueue"); //Analog Send MSG (1 and 2 alternately sent)             for(inti =1; I < One; i++) {MSG message=NewMSG (i%2==0?"1":"2",NewXMLMessageFormatter (NewType[] {typeof(string) }));

Send first parameter: The message object being sent; the second argument: Label
  Msgqueue.send (message, I  %  2   = =  0    " 1   " :   " 2   "  
            }            // asynchronous receive            var await GetMessage ("1");             var await GetMessage ("2");             = content1;             = Content2;}}    }

Message Queue Basic Usage instructions

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.