欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.Net消息隊(duì)列的使用方法

 更新時(shí)間:2014年02月11日 16:11:00   作者:  
這篇文章主要介紹了.Net消息隊(duì)列的使用方法,需要的朋友可以參考下

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

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

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


 

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

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

        }
        private void sendSimpleMsg()
        {
            //實(shí)例化MessageQueue,并指向現(xiàn)有的一個(gè)名稱為VideoQueue隊(duì)列
            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)有的一個(gè)名稱為VideoQueue隊(duì)列
            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 receiveComplexMsg<T>()
        {
            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)建消息隊(duì)列
        /// </summary>
        /// <param name="name">消息隊(duì)列名稱</param>
        /// <returns></returns>
        public void CreateNewQueue(string name)
        {
            if (!System.Messaging.MessageQueue.Exists(".\\private$\\" + name))//檢查是否已經(jīng)存在同名的消息隊(duì)列
            {

                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);//刪除一個(gè)消息隊(duì)列
                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);
        }
    }
}

相關(guān)文章

最新評(píng)論