. How NET message queues are used

Source: Internet
Author: User
Tags datetime file system message queue net message queue tostring

This article is mainly introduced. NET Message queue usage, the need for friends can refer to the following

.   NET uses Message Queuing to store a series of tasks to complete without using the same queue for programs to facilitate data sharing and collaboration among different programs ... In my experience, this is similar in some way to the session (there are many different aspects, of course), the same: session can store information in the Aspnet_state service, the site recompile or restart the site,   Session is not lost (the session timeout is normal, except in this case). Win7 to install Message Queuing components, other operating systems please Baidu search for relevant information. If the service does not start automatically, you need to start the service:   Create queues, then use queues, messages in queues, send one more, receive one less, and advance out first. The   code is as follows: using System; Using System.Collections.Generic; Using System.Linq; Using System.Web; Using System.Web.UI; Using System.Web.UI.WebControls; Using System.Messaging; Add a reference to the physical file System.Messaging namespace TestWeb {    public partial class MSMQtest:System.Web.UI.Page     {        protected void Page_Load (object sender, EventArgs e)         {&N Bsp          //createnewqueue ("Msgqueue");//Create a message queue            //sen Dsimplemsg ()///Every queue is best to send and receive information in the same format, otherwise it is difficult to convert the format.            //receivesimplemsg ();//           //receivesimpl Emsg ();            //sendcomplexmsg ();            //receivecomplexmsg ();             Msgmodel m = receivecomplexmsg<msgmodel> ();             Response.Write (m.tostring ());           {        private void sendsimplemsg ()         {           //materialized MessageQueue, and point to an existing one called Videoqueue queue           & nbsp MessageQueue MQ = new MessageQueue (@ ". Private$msgqueue");            //MQ. Send ("Message Test", "test Message");             System.Messaging.Message message = new System.Messaging.Message ();             message. Label = "message lable";             message. BODY = "message body";             MQ. Send (message);               Response.Write ("Send message successfully," + DateTime.Now + "<br/>");        }         private void receivesimplemsg ()         {&N Bsp     &NBSP     MessageQueue MQ = new MessageQueue (@ ". Private$msgqueue");            //Call MessageQueue Receive method receiving message             if (MQ). GetAllMessages (). Length > 0)             {                System. Messaging.message message = MQ. Receive (Timespan.fromseconds (5));                 if (message!= null)             &NB Sp   {                   //message. Formatter = new System.Messaging.XmlMessageFormatter (new string[] {"Message.bussiness.videopath,message"});//Message type conversion                     message. Formatter = new System.Messaging.XmlMessageFormatter (new type[] {typeof (String)});                     Response.Write (string. Format ("Receive eliminationSuccess,lable:{0},body:{1},{2}<br/> ", message. Label, message. Body.tostring (), DateTime.Now));                            }             Else             {              &N Bsp Response.Write ("No news!") <br/> ");                     {        private void sendcom Plexmsg ()         {           //Instantiate MessageQueue, and point to an existing name called the Videoqueue team Columns             MessageQueue MQ = new MessageQueue (@ ". Private$msgqueue");            //MQ. Send ("Message Test", "test Message");             System.Messaging.Message message = new System.Messaging.Message ();             message. Label = "complex message lable";       &NBsp     message. BODY = new Msgmodel ("1", "message 1");             MQ. Send (message);               Response.Write ("Send message successfully," +datetime.now+ "<br/>");        }         private void receivecomplexmsg ()         {& nbsp           MessageQueue MQ = new MessageQueue (@ ". Private$msgqueue");            //Call MessageQueue Receive method receiving message             if (MQ). GetAllMessages (). Length > 0)             {                System. Messaging.message message = MQ. Receive (Timespan.fromseconds (5));                 if (message!= null)             &NB Sp   {                    message. Formatter = new SYstem. Messaging.xmlmessageformatter (new type[] {typeof (Msgmodel)});/message type conversion               & nbsp     Msgmodel msg = (msgmodel) message. Body;                     Response.Write (string. Format ("Receive Message succeeded,lable:{0},body:{1},{2}<br/>", messages. Label, MSG, DateTime.Now));                            }             Else             {              &N Bsp Response.Write ("No news!") <br/> ");                     {        Private T receivecom Plexmsg<t> ()         {            MessageQueue MQ = new messagequ Eue (@ ". Private$msgqueue");            //Calling MessageQueue Receive method receiving message &nbsP           if (MQ. GetAllMessages (). Length > 0)             {                System. Messaging.message message = MQ. Receive (Timespan.fromseconds (5));                 if (message!= null)             &NB Sp   {                    message. Formatter = new System.Messaging.XmlMessageFormatter (new type[] {typeof (T)});/message type conversion                     T msg = (t) message. Body;                     return msg;                            }               return default (T);        }          ///<summary>     &NBSP  ///Create message queues        ///</summary>        ///<param name= "name" &G t; Message queue name </param>        ///<returns></returns>         Public void Createnewqueue (string name)         {            if (! System.Messaging.MessageQueue.Exists (". private$" + name)//check if Message Queuing with the same name already exists             {& nbsp                 System.Messaging.MessageQueue MQ = System.Messaging.MessageQueue. Create (". private$" + name);                 MQ. Label = "private$" +name;                 Response.Write ("Create success!") <br/> ");                         else         &NB Sp   {               //system.messaging.messagequeue.delete (". private$" + name);/delete a message queue               & nbsp Response.Write ("already exists <br/>");                     {     }     [Serializab Le]     public class Msgmodel    :         public string ID {get; set;}         public string Name {get; set:         public Msgmodel () {}     &nbs P   Public Msgmodel (string _id, string _name)         {            I D = _id;             Name = _name;        }         public override string ToString ()         {             if (string. IsNullOrEmpty (ID) | | String. IsNullOrEmpty (Name)) return "";             returnString. Format ("Id--{0},name--{1}", Id,name);        }    }}  

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.