1. 程式人生 > >MSMQ消息隊列

MSMQ消息隊列

style 解耦 沒有 先進先出 helper inpu spa sage XML

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 + "\n";
                };
                this.Dispatcher.BeginInvoke(hide);
            }
        }

        private void btn_ShowWindows1_Click(object sender, RoutedEventArgs e)
        {
            Window1 window = new Window1();
            window.Show();
        }
    }
}

  運行,我們用send手動給消息隊列發送消息

  並每隔十秒接收一次(outputText展示)

  消息隊列的消息被接收後就會清空,所以,當我們再打開一個窗口一起接收,消息就會分散開來給兩個窗口

MSMQ消息隊列