.net msmq消息隊列實例詳解
本文為大家分享了.net msmq消息隊列實例代碼,供大家參考,具體內(nèi)容如下
1.msmq消息隊列windows環(huán)境安裝
控制面板----》程序和功能----》啟用或關(guān)閉Windows程序----》Microsoft Message Queue(MSMQ)服務(wù)器

選中如圖所示功能點擊“確認”進行安裝,安裝好后可在 “計算機管理”中進行查看

2.創(chuàng)建消息隊列實體對象
/// <summary>
/// 消息實體
/// </summary>
[Serializable]
public class MsmqData
{
public int Id { get; set; }
public string Name { get; set; }
}
實體對象必須可序列化,即需添加[Serializable]
3.創(chuàng)建消息隊列管理對象
/// <summary>
/// 消息隊列管理對象
/// </summary>
public class MSMQManager
{
/// <summary>
/// 消息隊列地址
/// </summary>
public string _path;
/// <summary>
/// 消息隊列對象
/// </summary>
public MessageQueue _msmq;
/// <summary>
/// 構(gòu)造函數(shù)并初始化消息隊列對象
/// </summary>
/// <param name="path"></param>
public MSMQManager(string path = null)
{
if (string.IsNullOrEmpty(path))
{
_path = ConfigurationManager.AppSettings["MsmqPath"].ToString();
}
else
{
_path = path;
}
if (MessageQueue.Exists(_path))
{
_msmq = new MessageQueue(_path);
}
else
{
_msmq = MessageQueue.Create(_path);
}
}
/// <summary>
/// 發(fā)送消息隊列
/// </summary>
/// <param name="body"></param>
public void Send(object body)
{
_msmq.Send(new Message(body, new XmlMessageFormatter(new Type[] { typeof(MsmqData) })));
}
/// <summary>
/// 接受隊列中第一個消息后刪除
/// </summary>
/// <returns></returns>
public object ReceiveMessage()
{
var msg = _msmq.Receive();
if (msg != null)
{
//msg.Formatter = new BinaryMessageFormatter();
msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(MsmqData) });
var body = (MsmqData)msg.Body;
Console.WriteLine("消息內(nèi)容:{0},{1}", body.Id, body.Name);
return msg.Body;
}
return null;
}
/// <summary>
/// 遍歷消息隊列中的消息并刪除
/// </summary>
public void WriteAllMessage()
{
var enumerator = _msmq.GetMessageEnumerator2();
while (enumerator.MoveNext())
{
Message msg = (Message)(enumerator.Current);
//msg.Formatter = new BinaryMessageFormatter();
msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(MsmqData) });
var body = (MsmqData)msg.Body;
Console.WriteLine("消息內(nèi)容:{0},{1}", body.Id, body.Name);
//根據(jù)消息ID查詢并刪除消息隊列
_msmq.ReceiveById(msg.Id);
}
}
}
此例中使用XML格式(XmlMessageFormtter)對消息進行格式化
4.主程序添加調(diào)用消息隊列
static void Main(string[] args)
{
var msmqManager = new MSMQManager();
for (int i = 1; i <= 10; i++)
{
MsmqData data = new MsmqData() { Id = i, Name = string.Format("Name{0}", i) };
//發(fā)送消息
msmqManager.Send(data);
}
var msg = msmqManager.ReceiveMessage();
msmqManager.WriteAllMessage();
Console.ReadLine();
}
添加消息隊列地址配置,本例使用私有隊列
<appSettings> <add key="MsmqPath" value=".\private$\myQueue"/> </appSettings>
5.運行程序查看結(jié)果
可以在發(fā)送完消息后打上斷點查看消息隊列消息正文

最后運行結(jié)果

6.常見消息隊列類型路徑的語法
隊列類型
路徑中使用的語法
公共隊列
MachineName\QueueName
專用隊列
MachineName\Private$\QueueName
日志隊列
MachineName\QueueName\Journal$
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery+Asp.Net實現(xiàn)省市二級聯(lián)動功能的方法
這篇文章主要介紹了jQuery+Asp.Net實現(xiàn)省市二級聯(lián)動功能的方法,涉及asp.net數(shù)據(jù)庫讀取與字符串轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
ASP.NET Core MVC/WebApi基礎(chǔ)系列1
這篇文章主要介紹了ASP.NET Core MVC/WebApi基礎(chǔ)系列,后續(xù)會穿插講EF Core和ASP.NET Core,雖說是基礎(chǔ)系列但也是也有你不知道的。2019-04-04

