. NET Message Queuing usage

Source: Internet
Author: User
Tags message queue

Using Message Queuing in your project, you can decouple the message provider from the message consumer and improve the flexibility and scalability of your system. Among them, enterprise-class Message Queuing middleware has IBM WebSphere MQ, Tibco EMS, HORNETQ, and so on. Microsoft's Windows system also comes with a Message Queuing feature that can be turned on in the "Start or shut down Windows features" :

Check that the Message Queuing service is running in the service after it is turned on:

The following is a simple Message Queuing operation:

usingSystem;usingsystem.messaging;namespacemq{classMqhelper {/// <summary>        ///Create a message queue/// </summary>        /// <param name= "name" >Name of message queue</param>        /// <returns>whether the creation was successful</returns>         Public Static BOOLCREATENEWMQ (stringname) {            if(! Messagequeue.exists (". \\private$\\"+name)) {MessageQueue MQ= Messagequeue.create (". \\private$\\"+name); Mq. Label="private$\\"+name; return true; }            Else            {                return false; }        }        /// <summary>        ///Delete a message queue/// </summary>        /// <param name= "name" >Name of message queue</param>        /// <returns>whether the creation was successful</returns>         Public Static BOOLDELETENEWMQ (stringname) {            if(! Messagequeue.exists (". \\private$\\"+name)) {Messagequeue.delete (". \\private$\\"+name); return true; }            Else            {                return false; }        }        /// <summary>        ///send a message to a specified message queue/// </summary>        /// <param name= "Mq_name" >Message Queue name</param>        /// <param name= "msg_lable" >message Header</param>        /// <param name= "Msg_body" >message body</param>         Public Static voidSendMessage (stringMq_name,stringMsg_lable,stringmsg_body) {MessageQueue MQ=NewMessageQueue (@". \private$\"+mq_name); Message Message=NewMessage (); Message. Label=msg_lable; Message. Body=Msg_body; Mq.        Send (message); }        /// <summary>        ///gets the first message from the specified message queue/// </summary>        /// <param name= "Mq_name" >Message Queue name</param>        /// <returns>Message</returns>         Public StaticMessage ReceiveMessage (stringmq_name) {MessageQueue MQ=NewMessageQueue (@". \private$\"+mq_name); if(MQ. GetAllMessages (). Length >0) {Message message=MQ.                Receive (); if(Message! =NULL) {message. Formatter=NewSystem.Messaging.XmlMessageFormatter (NewType[] {typeof(string) }); }                returnmessage; }            Else            {                return NULL; }        }    }}

To invoke a well-written method in WPF:

usingSystem;usingsystem.timers;usingSystem.Windows;namespacemq{ Public Partial classMainwindow:window {Private Static ReadOnly stringMq_name ="TEXTMQ"; PrivateTimer timer;  PublicMainWindow () {InitializeComponent ();            MQHELPER.CREATENEWMQ (Mq_name); Timer=NewTimer (); Timer. Interval= +; Timer. Elapsed+=NewSystem.Timers.ElapsedEventHandler (Flash); Timer.        Start (); }        Private voidButton_Click (Objectsender, RoutedEventArgs e) {            stringMq_content =Inputtext.text; //send a message to the message queueMqhelper.sendmessage (Mq_name,"Test", mq_content); }        //loops through the message in the message queue and presents it in a textbox        Private voidFlashObjectsender, Elapsedeventargs e) {            varMessage =mqhelper.receivemessage (mq_name); if(Message! =NULL) {Action Hide=Delegate() {Outputtext.text+ = message. Id +" "+ message. Label +" "+ message. Body +"\ n";                };  This.            Dispatcher.begininvoke (hide); }        }    }}

Operation Result:

. NET Message Queuing usage

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.