最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解C#如何使用消息隊(duì)列MSMQ

 更新時(shí)間:2024年01月05日 08:40:02   作者:zhaotianff  
消息隊(duì)列 (MSMQ Microsoft Message Queuing)是MS提供的服務(wù),也就是Windows操作系統(tǒng)的功能,下面就跟隨小編一起了解一下C#中是如何使用消息隊(duì)列MSMQ的吧

最近項(xiàng)目用到消息隊(duì)列,找資料學(xué)習(xí)了下。把學(xué)習(xí)的結(jié)果 分享出來

首先說一下,消息隊(duì)列 (MSMQ Microsoft Message Queuing)是MS提供的服務(wù),也就是Windows操作系統(tǒng)的功能,并不是.Net提供的。

MSDN上的解釋如下:

Message Queuing (MSMQ) technology enables applications running at different times to communicate across heterogeneous networks and systems that may be temporarily offline.

Applications send messages to queues and read messages from queues.

The following illustration shows how a queue can hold messages that are generated by multiple sending applications and read by multiple receiving applications.

消息隊(duì)列(MSMQ)技術(shù)使得運(yùn)行于不同時(shí)間的應(yīng)用程序能夠在各種各樣的網(wǎng)絡(luò)和可能暫時(shí)脫機(jī)的系統(tǒng)之間進(jìn)行通信。

應(yīng)用程序?qū)⑾l(fā)送到隊(duì)列,并從隊(duì)列中讀取消息。

下圖演示了消息隊(duì)列如何保存由多個(gè)發(fā)送應(yīng)用程序生成的消息,并被多個(gè)接收應(yīng)用程序讀取。

消息一旦發(fā)送到隊(duì)列中,便會(huì)一直存在,即使發(fā)送的應(yīng)用程序已經(jīng)關(guān)閉。

 MSMQ服務(wù)默認(rèn)是關(guān)閉的,(Window7及以上操作系統(tǒng))按以下方式打開

1、打開運(yùn)行,輸入"OptionalFeatures",鉤上Microsoft Message Queue(MSMQ)服務(wù)器。

(Windows Server 2008R2及以上)按以下方式打開

2、打開運(yùn)行,輸入"appwiz.cpl",在任務(wù)列表中選擇“打開或關(guān)閉Windows功能”

然后在"添加角色"中選擇消息隊(duì)列

 消息隊(duì)列分為以下幾種,每種隊(duì)列的路徑表示形式如下:

公用隊(duì)列 MachineName\QueueName

專用隊(duì)列 MachineName\Private$\QueueName

日記隊(duì)列 MachineName\QueueName\Journal$

計(jì)算機(jī)日記隊(duì)列 MachineName\Journal$

計(jì)算機(jī)死信隊(duì)列 MachineName\Deadletter$

計(jì)算機(jī)事務(wù)性死信隊(duì)列 MachineName\XactDeadletter$

這里的MachineName可以用 “."代替,代表當(dāng)前計(jì)算機(jī)

 需要先引用System.Messaging.dll

創(chuàng)建消息隊(duì)列

//消息隊(duì)列路徑
            string path = ".\\Private$\\myQueue";
            MessageQueue queue;
            //如果存在指定路徑的消息隊(duì)列
            if(MessageQueue.Exists(path))
            {
                //獲取這個(gè)消息隊(duì)列
                queue = new MessageQueue(path);
            }
            else
            {
                //不存在,就創(chuàng)建一個(gè)新的,并獲取這個(gè)消息隊(duì)列對(duì)象
                queue = MessageQueue.Create(path);
            }

發(fā)送字符串消息

System.Messaging.Message msg = new System.Messaging.Message();
                    //內(nèi)容
                    msg.Body = "Hello World";
                    //指定格式化程序
                    msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                    queue.Send(msg);

發(fā)送消息的時(shí)候要指定格式化程序,如果不指定,格式化程序?qū)⒛J(rèn)為XmlMessageFormatter(使用基于 XSD 架構(gòu)定義的 XML 格式來接收和發(fā)送消息。)

接收字符串消息

//接收到的消息對(duì)象
              System.Messaging.Message msg = queue.Receive();
              //指定格式化程序
              msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
              //接收到的內(nèi)容
              string str = msg.Body.ToString();

 發(fā)送二進(jìn)制消息(如圖像)

引用 System.Drawing.dll

System.Drawing.Image image = System.Drawing.Bitmap.FromFile("a.jpg");
Message message = new Message(image, new BinaryMessageFormatter());          
queue.Send(message);

接收二進(jìn)制消息

System.Messaging.Message message = queue.Receive();
System.Drawing.Bitmap image= (System.Drawing.Bitmap)message.Body;       
3image.Save("a.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

獲取消息隊(duì)列的消息的數(shù)量 

int num  = queue.GetAllMessages().Length;

清空消息隊(duì)列

queue.Purge();

以圖形化的方式查看消息隊(duì)列中的消息

運(yùn)行輸入 compmgmt.msc,打開計(jì)算機(jī)管理,選擇[服務(wù)和應(yīng)用程序-消息隊(duì)列]。只要消息隊(duì)列中的消息沒有被接收,就可以在這里查看得到。

到此這篇關(guān)于詳解C#如何使用消息隊(duì)列MSMQ的文章就介紹到這了,更多相關(guān)C#消息隊(duì)列MSMQ內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

大丰市| 济阳县| 凤阳县| 山西省| 宜宾县| 鄢陵县| 桂东县| 连平县| 佛冈县| 濮阳县| 南平市| 遂川县| 郧西县| 涟水县| 永登县| 花垣县| 余姚市| 平山县| 新晃| 开阳县| 微山县| 汽车| 崇州市| 峡江县| 墨脱县| 桐乡市| 呼图壁县| 建平县| 宜宾市| 大城县| 巫山县| 辛集市| 即墨市| 镇康县| 井陉县| 岱山县| 华亭县| 泉州市| 汉寿县| 柯坪县| 中阳县|