主頁 > 知識庫 > .Net消息隊列的使用方法

.Net消息隊列的使用方法

熱門標(biāo)簽:廣州銷售外呼系統(tǒng)定制 云狐人工智能電話機(jī)器人 電銷機(jī)器人 數(shù)據(jù) ai電銷機(jī)器人對貸款有幫助嗎 福州人工智能電銷機(jī)器人加盟 地圖標(biāo)注多少錢一張 怎樣給陜西地圖標(biāo)注顏色 宿遷智能外呼系統(tǒng)排名 400電話辦理信任翰諾科技

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

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

win7中安裝消息隊列組件,其他操作系統(tǒng)請百度搜索相關(guān)資料。


 

如果服務(wù)沒有自動啟動,需要啟動服務(wù):

先創(chuàng)建隊列,再使用隊列,隊列中的消息,發(fā)送一個多一個,接收一個少一個,先進(jìn)先出。

復(fù)制代碼 代碼如下:

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");//創(chuàng)建一個消息隊列
            //sendSimpleMsg();//每一個隊列最好只發(fā)送和接收同一種格式的信息,不然不好轉(zhuǎn)換格式。
            //receiveSimpleMsg();//
            //receiveSimpleMsg();
            //sendComplexMsg();
            //receiveComplexMsg();
            MsgModel m = receiveComplexMsgMsgModel>();
            Response.Write(m.ToString());

        }
        private void sendSimpleMsg()
        {
            //實(shí)例化MessageQueue,并指向現(xiàn)有的一個名稱為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("成功發(fā)送消息," + DateTime.Now + "br/>");
        }
        private void receiveSimpleMsg()
        {
            MessageQueue MQ = new MessageQueue(@".\private$\MsgQueue");
            //調(diào)用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" });//消息類型轉(zhuǎn)換
                    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()
        {
            //實(shí)例化MessageQueue,并指向現(xiàn)有的一個名稱為VideoQueue隊列
            MessageQueue MQ = new MessageQueue(@".\private$\MsgQueue");
            //MQ.Send("消息測試", "測試消息");
            System.Messaging.Message message = new System.Messaging.Message();
            message.Label = "復(fù)雜消息lable";
            message.Body = new MsgModel("1", "消息1");
            MQ.Send(message);

            Response.Write("成功發(fā)送消息,"+DateTime.Now+"br/>");
        }
        private void receiveComplexMsg()
        {
            MessageQueue MQ = new MessageQueue(@".\private$\MsgQueue");
            //調(diào)用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) });//消息類型轉(zhuǎn)換
                    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 receiveComplexMsgT>()
        {
            MessageQueue MQ = new MessageQueue(@".\private$\MsgQueue");
            //調(diào)用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) });//消息類型轉(zhuǎn)換
                    T msg = (T)message.Body;
                    return msg;
                }
            }

            return default(T);
        }

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

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

您可能感興趣的文章:
  • RabbitMQ .NET消息隊列使用詳解
  • .net msmq消息隊列實(shí)例詳解

標(biāo)簽:焦作 大興安嶺 曲靖 綿陽 新疆 延安 宜春 黃南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《.Net消息隊列的使用方法》,本文關(guān)鍵詞  .Net,消息,隊列,的,使用方法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《.Net消息隊列的使用方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于.Net消息隊列的使用方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章