好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

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

.Net消息隊列的使用方法

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

.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 = receiveComplexMsgMsgModel>();
            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 receiveComplexMsgT>()
        {
            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);
        }
    }
}

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

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

巨人網絡通訊聲明:本文標題《.Net消息隊列的使用方法》,本文關鍵詞  .Net,消息,隊列,的,使用方法,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《.Net消息隊列的使用方法》相關的同類信息!
  • 本頁收集關于.Net消息隊列的使用方法的相關信息資訊供網民參考!
  • 推薦文章