A little experience on accessing the MSMQ remote private queue

Source: Internet
Author: User
Tags msmq

The private queue should be called "private queue" as if it were more appropriate, O (∩_∩) o

There is no doubt that you can access the private queue of the remote host's MSMQ, but it is important to note that you cannot create a private queue through code, and I don't know why.

Here's my experience. 1, first of all to ensure that the remote host and local machine joined to the same domain at the same time 2, to create a private queue in the remote host through administrative tools, such as 192.168.117.47\private$\mypath, you can specify whether to enable transaction 3 at the time of creation, If the specified private queue is not available locally, and the specified private queue is enabled for transactions, it is best to know explicitly that the remote has created this private queue and that the private queue has been enabled for transaction 4, and the format of the remote private queue is given below if the IP Address form, use "FORMATNAME:DIRECT=TCP:" + Remote IP + @ "\private$" + @ "\" + Private queue path name if this is the way the machine name is, use "Formatname:direct=os:" + Remote Master Machine name +@ "\private$" + @ "\" + path name of private queue5, to ensure that the machine and the remote host use the same domain account login, it is best this account is also the local and remote computer system administrators group members send messages public bool SendMessage (string path, object source, BOOL transactional = False) {//Note the format of the path is shown in the section of this log (i)if (!string. Isnullorwhitespace (path) && Source = null) {try {u Sing (MessageQueue mqsender = newMessageQueue (path)){mqSender.MessageReadPropertyFilter.Body = true;                        MqSender.MessageReadPropertyFilter.AppSpecific = true;                        MqSender.MessageReadPropertyFilter.Priority = true; MqSender.MessageReadPropertyFilter.Recoverable = true; Prevent loss of message when restarting host Mqsender.formatter = new XMLMessageFormatter (new type[] {source.                        GetType ()}); if (transactional = = true)                        {          &N Bsp                 using (messagequeuetransaction tran = new MessageQueueTransaction ( )                             {                              Tran  . Begin ();                                MQ Sender.send (source, Tran);                          &NBSP ;     Tran. Commit ();                           }                       }              &N Bsp     &NBSP;   else                         /  Mqsender.send (source);                        mqsender.close ();  &nbsp ;                }                  &N Bsp Return true;               }              &NBS P catch (Messagequeueexception ex)                 {        &NBSP ;           Console.Write (ex);               }  &nbsp ;             catch (Exception ex)                 {&NB Sp                   Console.Write (ex);            &nbSp  }           }            return false;    &N Bsp  }  Receive message  public static T getmessage<t> (string path, bool Isdeletemessage = true, bool Transactional = False)         {            T result = Default (t);        & nbsp   try            {              using (messag Equeue mqreceiver = new MessageQueue (path))                 {      & nbsp             MqReceiver.MessageReadPropertyFilter.Body = true;        &NB Sp           mqReceiver.MessageReadPropertyFilter.AppSpecific = true;        &N Bsp           mqReceiver.MessageReadPropertyFilter.Priority = true;        &NBSP ;           mqReceiver.MessageReadPropertyFilter.Recoverable = true; Prevent loss of messages when restarting hosts                     Mqreceiver.formatter = new XMLMESSAGEF Ormatter (new type[] {typeof (T)});                    Message message = null;                    if (transactional  == true)                     {              &N Bsp         if (Isdeletemessage = true)                   &NB Sp     {                            using (Me Ssagequeuetransaction tran = new MessageQueueTransaction ())                             {                        &N Bsp       Tran. Begin ();                                Co. Nsole. WriteLine ("Waiting to receive ...");                                message = Mqreceiver.receive (tran);              &NBsp                 Console.WriteLine ("Received");                                Tran. Commit ();                           }                       }              &N Bsp         else                            message = Mqreceiver.peek ();                   }    & nbsp               else                    {                        if (isdeletemessage)     &N Bsp                  {                            CONSOLE.W Riteline ("Waiting to receive ...");                            m Essage = mqreceiver.receive ();                          and nbsp Console.WriteLine ("Received");                       }  &nbsp ;                     else                            message = Mqreceiver.peek ();            &N Bsp      }                    if (message! = NULL)   &NB Sp                     result = (T) message. body;                    mqreceiver.close ();               }  &NB Sp        }            catch (Messagequeueexception ex)     &NBSP ;       {                Console.Write (ex);      &NBSP ;    }            catch (Exception ex)             {&N Bsp               Console.Write (ex);           }  &nbsp ;         return result;       } : The following code will appear exception bool B = Messagequeue.exi when debugging a remote host STS (path); Supposedly not, but my machine debugging always error, do not know why?messagequene m = messagequene.create (path);//I don't know, it didn't pass anyway.BOOL B = m.transactional; It seems that this property is not supported at the far end. There is no problem with the above information in the local, O (∩_∩) o~: The following is only valid locally, do not know whether the remote is valid, did not try to get the local private queue of the number of messages (the main code is as follows:) using         System.Diagnostics; Return (long) (New PerformanceCounter ("MSMQ Queue", "Messages in Queue", path). NextValue ()), execute the command "LODCTR/R" in DOS as an administrator, or through the command "perfmon" if InvalidOperationException is missing from the registry when executing the above code View performance counters also indicates that in path the local machine name to give full such as @ "Mypc\private$\mypath", can not use the ellipsis @ ". \private$\mypath" override in the programming of MSMQ, add a reference System.Message.dll and add a reference to the corresponding namespace in fact, in addition to MSMQ, we still have a lot of other options, such as ActiveMQ and so on, there are interested to see you ... about using queue 1 in a cluster, to use queues in a cluster, create your own queue in the private queue of each host in the cluster (preferably create a transactional queue), for example, my cluster contains two hosts 192.168.117.47, 192.168.117.48, the common drift address is 192.168.117.50, and I'll create the \private$\mypath in the private queue of each host's MSMQ (specified with transaction when created)2. After creating the queue, specify the user and the user's access to the queue in the properties of the queue, otherwise the client program that accesses the queue will not be able to send and receive the queue correctly. 3, when sending messages, to use the drift address 192.168.117.50 send, also specifyWhen you send a message Enable transaction4. When receiving the message, use the stand-alone IP 192.168.117.47 or 192.168.117.48 receive queue, and please Don'tSpecify the queue to receive the message to enable transactions, hey, here is not and not in the cluster when the situation is somewhat different, but also to send a message some of the same?! 5. The code given before is XMLMessageFormatter, which requires that the type of the object in the message must be known when the message is received, if we specify Messagequeue.formatter = new when sending and receiving the message BinaryMessageFormatter (), you can send and receive messages without knowing the type of objects contained in the message before the receive is a synchronous receive message, there is no way to get the message asynchronously? Of course, the code is as follows: The keynote side code: MessageQueue MQ = getmessagequeue (path); Getmessagequeue function How to implement it will not write it?! Mq. receivecompleted + = new Receivecompletedeventhandler (mq_receivecompleted); MQ. BeginReceive (); callback function private voidmq_receivecompleted (object sender, Receivecompletedeventargs e){MessageQueue MQ = sender as MessageQueue; if (MQ! = NULL && E! = null) {Message message = MQ.                EndReceive (E.asyncresult); if (message! = NULL) Console.WriteLine (message.                Body); Mq.            BeginReceive (); This is the code that is deleted after the message is received, and of course it can be used to receive messages but not delete them.

A little experience on accessing the MSMQ remote private queue

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.