MSMQ 学习(1)

在 Windows Server 2008 or Windows Server 2008 R2 上安装消息队列 4.0

  1. 在服务器管理器中,单击“功能”

  2. “功能摘要”下的右窗格中,单击“添加功能”

  3. 在生成的窗口中,展开“消息队列”

  4. 展开“消息队列服务”

  5. 单击“目录服务集成”(用于加入域的计算机),然后单击“HTTP 支持”

  6. 单击“下一步”,然后单击“安装”

在 Windows 7 或 Windows Vista 上安装消息队列 4.0

  1. 打开“控制面板”

  2. 单击“程序”,然后在“程序和功能”下单击“打开或关闭 Windows 功能”

  3. 展开“Microsoft Message Queue (MSMQ) 服务器”,展开“Microsoft Message Queue (MSMQ) 服务器核心”,然后选中对应于以下要安装的“消息队列”功能的复选框:

    • MSMQ Active Directory 域服务集成(用于加入域的计算机)。

    • MSMQ HTTP 支持。

  4. 单击“确定”

  5. 如果提示您重新启动计算机,请单击“确定”完成安装。

在 Windows XP 和 Windows Server 2003 上安装消息队列 3.0

  1. 打开“控制面板”

  2. 单击“添加/删除程序”,然后单击“添加/删除 Windows 组件”

  3. 选择“消息队列”并单击“详细信息”

    System_CAPS_note注意

    如果运行的是 Windows Server 2003,请选择“应用程序服务器”来访问消息队列。

  4. 确保在详细信息页上已选中“MSMQ HTTP 支持”选项。

  5. 单击“确定”退出详细信息页,然后单击“下一步”完成安装。

  6. 如果提示您重新启动计算机,请单击“确定”完成安装。

using System.Messaging

//生产者

class Producer
{
static void Main(string[] args)
{
string mqName = ".\Private$\SendShortMessageQueue";
if (!MessageQueue.Exists(mqName))
{
MessageQueue.Create(mqName);
}
var queue=new MessageQueue(mqName);

var msg=new Message();
msg.Body = "尊敬的先生,你的快递今日到达";
msg.Label = "25623015";
msg.Formatter=new XmlMessageFormatter(new Type[]{typeof(string)});
queue.Send(msg);
Console.Read();
}
}

//消费者

class Customer
{
static void Main(string[] args)
{
string mqName = ".\Private$\SendShortMessageQueue";
MessageQueue queue=new MessageQueue(mqName);
queue.Formatter=new XmlMessageFormatter(new Type[]{typeof(string)});
TimeSpan timeOut=new TimeSpan(0,0,0,3);
while (Console.ReadLine() != "exit")
{
Message msg=null;
try
{
msg = queue.Receive(timeOut);
}
catch
{

}
if(msg !=null)
Console.WriteLine("获取的消息标题是{0},内容是{1}",msg.Label,msg.Body as string);
else
{
Console.WriteLine("暂时没有新消息...");
}
}
}
}

原文地址:https://www.cnblogs.com/zhshlimi/p/5497338.html