程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> .Net消息隊列的使用方法

.Net消息隊列的使用方法

編輯:ASP.NET基礎

.Net使用消息隊列,借助windows組件來存儲要完成的一系列任務,不用程序使用同一個隊列,方便不同程序之間的數據共享和協作……

以本人經驗,這個在某個方面類似於session(當然還有很多方面不同),相同之處:session可以把信息存儲在aspnet_state服務中,網站重新編譯或者重新啟動網站,session不會丟失(session超時是正常情況,這種情況除外)。

win7中安裝消息隊列組件,其他操作系統請百度搜索相關資料。


 

如果服務沒有自動啟動,需要啟動服務:

先創建隊列,再使用隊列,隊列中的消息,發送一個多一個,接收一個少一個,先進先出。

復制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Messaging;
//添加物理文件 System.Messaging 的引用
namespace testweb
{
    public partial class MSMQtest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //CreateNewQueue("MsgQueue");//創建一個消息隊列
            //sendSimpleMsg();//每一個隊列最好只發送和接收同一種格式的信息,不然不好轉換格式。
            //receiveSimpleMsg();//
            //receiveSimpleMsg();
            //sendComplexMsg();
            //receiveComplexMsg();
            MsgModel m = receiveComplexMsg<MsgModel>();
            Response.Write(m.ToString());

        }
        private void sendSimpleMsg()
        {
            //實例化MessageQueue,並指向現有的一個名稱為VideoQueue隊列
            MessageQueue MQ = new MessageQueue(@".\private$\MsgQueue");
            //MQ.Send("消息測試", "測試消息");
            System.Messaging.Message message = new System.Messaging.Message();
            message.Label = "消息lable";
            message.Body = "消息body";
            MQ.Send(message);

            Response.Write("成功發送消息," + DateTime.Now + "<br/>");
        }
        private void receiveSimpleMsg()
        {
            MessageQueue MQ = new MessageQueue(@".\private$\MsgQueue");
            //調用MessageQueue的Receive方法接收消息
            if (MQ.GetAllMessages().Length > 0)
            {
                System.Messaging.Message message = MQ.Receive(TimeSpan.FromSeconds(5));
                if (message != null)
                {
                    //message.Formatter = new System.Messaging.XmlMessageFormatter(new string[] { "Message.Bussiness.VideoPath,Message" });//消息類型轉換
                    message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
                    Response.Write(string.Format("接收消息成功,lable:{0},body:{1},{2}<br/>", message.Label, message.Body.ToString(), DateTime.Now));
                }
            }
            else
            {
                Response.Write("沒有消息了!<br/>");
            }
        }
        private void sendComplexMsg()
        {
            //實例化MessageQueue,並指向現有的一個名稱為VideoQueue隊列
            MessageQueue MQ = new MessageQueue(@".\private$\MsgQueue");
            //MQ.Send("消息測試", "測試消息");
            System.Messaging.Message message = new System.Messaging.Message();
            message.Label = "復雜消息lable";
            message.Body = new MsgModel("1", "消息1");
            MQ.Send(message);

            Response.Write("成功發送消息,"+DateTime.Now+"<br/>");
        }
        private void receiveComplexMsg()
        {
            MessageQueue MQ = new MessageQueue(@".\private$\MsgQueue");
            //調用MessageQueue的Receive方法接收消息
            if (MQ.GetAllMessages().Length > 0)
            {
                System.Messaging.Message message = MQ.Receive(TimeSpan.FromSeconds(5));
                if (message != null)
                {
                    message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(MsgModel) });//消息類型轉換
                    MsgModel msg = (MsgModel)message.Body;
                    Response.Write(string.Format("接收消息成功,lable:{0},body:{1},{2}<br/>", message.Label, msg, DateTime.Now));
                }
            }
            else
            {
                Response.Write("沒有消息了!<br/>");
            }
        }
        private T receiveComplexMsg<T>()
        {
            MessageQueue MQ = new MessageQueue(@".\private$\MsgQueue");
            //調用MessageQueue的Receive方法接收消息
            if (MQ.GetAllMessages().Length > 0)
            {
                System.Messaging.Message message = MQ.Receive(TimeSpan.FromSeconds(5));
                if (message != null)
                {
                    message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(T) });//消息類型轉換
                    T msg = (T)message.Body;
                    return msg;
                }
            }

            return default(T);
        }

        /// <summary>
        /// 創建消息隊列
        /// </summary>
        /// <param name="name">消息隊列名稱</param>
        /// <returns></returns>
        public void CreateNewQueue(string name)
        {
            if (!System.Messaging.MessageQueue.Exists(".\\private$\\" + name))//檢查是否已經存在同名的消息隊列
            {

                System.Messaging.MessageQueue mq = System.Messaging.MessageQueue.Create(".\\private$\\" + name);
                mq.Label = "private$\\"+name;
                Response.Write("創建成功!<br/>");
            }
            else
            {
                //System.Messaging.MessageQueue.Delete(".\\private$\\" + name);//刪除一個消息隊列
                Response.Write("已經存在<br/>");
            }
        }

    }
    [Serializable]
    public class MsgModel
    {
        public string id { get; set; }
        public string Name { get; set; }
        public MsgModel() { }
        public MsgModel(string _id, string _Name)
        {
            id = _id;
            Name = _Name;
        }
        public override string ToString()
        {
            if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(Name)) return "";
            return string.Format("id--{0},Name--{1}",id,Name);
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved