1.什么是消息队列
消息队列是消息的传输过程中保存消息的容器。
在项目中使用消息队列,可以对消息提供方和消息消费方进行解耦,提高系统的灵活性和可拓展性。
先创建队列,再使用队列,队列中的消息,发送一个多一个,接收一个少一个,先进先出。
2.安装
安装完后,打开任务管理器->服务
看看有没有这两个东西(消息队列、消息队列触发器),如果没有自动运行,就手动给跑一下
3.创建消息队列
这里使用wcf演示
新建wcf 应用
创建一个类MQHelper
using System; using System.Messaging; namespace WpfApp1 { public class MQHelper { /// <summary> /// 创建一个消息队列 /// </summary> /// <param name="name">消息队列的名称</param> /// <returns>是否创建成功</returns> public static bool CreateNewMQ(string name) { if (!MessageQueue.Exists(".\private$\" + name)) { MessageQueue mq = MessageQueue.Create(".\private$\" + name); mq.Label = "private$\" + name; return true; } else { return false; } } /// <summary> /// 删除一个消息队列 /// </summary> /// <param name="name">消息队列的名称</param> /// <returns>是否创建成功</returns> public static bool DeleteNewMQ(string name) { if (!MessageQueue.Exists(".\private$\" + name)) { MessageQueue.Delete(".\private$\" + name); return true; } else { return false; } } /// <summary> /// 发送消息到指定消息队列 /// </summary> /// <param name="mq_name">消息队列名称</param> /// <param name="msg_lable">消息头</param> /// <param name="msg_body">消息体</param> public static void SendMessage(string mq_name, string msg_lable, string msg_body) { MessageQueue mq = new MessageQueue(@".private$" + mq_name); Message message = new Message(); message.Label = msg_lable; message.Body = msg_body; mq.Send(message); } /// <summary> /// 从指定消息队列获取第一条消息 /// </summary> /// <param name="mq_name">消息队列名称</param> /// <returns>Message</returns> public static Message ReceiveMessage(string mq_name) { MessageQueue mq = new MessageQueue(@".private$" + mq_name); if (mq.GetAllMessages().Length > 0) { Message message = mq.Receive(); if (message != null) { message.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) }); } return message; } else { return null; } } } }
设计MainWindows
<Grid> <TextBox HorizontalAlignment="Left" Height="26" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="681" Name="inputText" Margin="5,3,0,0"/> <Button Content="Send" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="699,7,0,0" Name="btn_Send" Click="btn_Send_Click"/> <TextBox HorizontalAlignment="Left" Height="370" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="678" Margin="4,36,0,0" Name="outputText"/> <Button Content="ShowWindows1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="701,54,0,0" x:Name="btn_ShowWindows1" Click="btn_ShowWindows1_Click"/> </Grid>
后台代码:
using System; using System.Timers; using System.Windows; namespace WpfApp1 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { private static readonly string mq_name = "textMQ"; public MainWindow() { InitializeComponent(); MQHelper.CreateNewMQ(mq_name); Timer timer = new Timer(10000); timer.Elapsed += new ElapsedEventHandler(flash); timer.Start(); } private void btn_Send_Click(object sender, RoutedEventArgs e) { string mq_content = inputText.Text; //发送一条消息到消息队列 MQHelper.SendMessage(mq_name, "test", mq_content); inputText.Text = ""; } //循环获取消息队列中的消息,并展现到TextBox中 private void flash(object sender, ElapsedEventArgs e) { var message = MQHelper.ReceiveMessage(mq_name); if (message != null) { Action hide = delegate () { outputText.Text += message.Id + " " + message.Label + " " + message.Body + " "; }; this.Dispatcher.BeginInvoke(hide); } } private void btn_ShowWindows1_Click(object sender, RoutedEventArgs e) { Window1 window = new Window1(); window.Show(); } } }
运行,我们用send手动给消息队列发送消息
并每隔十秒接收一次(outputText展示)
消息队列的消息被接收后就会清空,所以,当我们再打开一个窗口一起接收,消息就会分散开来给两个窗口