ASP.NET ActiveMQ 消息隊列


1.引入

2.發送消息

3.接收消息

概述:MQ消息存放在內存,重啟后,消息丟失。接收后,消息丟失(只取一次),不取,一直在且速度快。

 

使用前:下載apache-activemq-5.15.2-bin   ,下載完運行activemq.bat(必須提前安裝Java sdk)

               下載Dll

注冊成功后:http://localhost:8161/admin/   用戶名:admin  密碼:admin  登錄 

 

 

 

一 引入DLL

 

二 發送消息、接收消息

 

using Apache.NMS;
using Apache.NMS.ActiveMQ;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformQuartz
{
    public partial class FrmMQ : Form
    {
        public FrmMQ()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var input = this.textBox1.Text.Trim();

            if(string.IsNullOrEmpty(input))
            {
                this.lblMsg.ForeColor = Color.Red;
                this.lblMsg.Text = "請輸入需要發送的消息";
                return;
            }

            Send();
        }

        public void Send()
        {
            ConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616");
            //通過工廠建立連接
            using (IConnection connection = factory.CreateConnection())
            {
                //通過連接創建Session會話
                using (ISession session = connection.CreateSession())
                {
                    //通過會話創建生產者,方法里面new出來的是MQ中的Queue
                    IMessageProducer prod = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("firstQueue"));
                    //創建一個發送的消息對象
                    ITextMessage message = prod.CreateTextMessage();
                    //給這個對象賦實際的消息
                    message.Text = this.textBox1.Text.Trim();
                    //設置消息對象的屬性,這個很重要哦,是Queue的過濾條件,也是P2P消息的唯一指定屬性
                    message.Properties.SetString("filter", "demo");
                    //生產者把消息發送出去,幾個枚舉參數MsgDeliveryMode是否長鏈,MsgPriority消息優先級別,發送最小單位,當然還有其他重載
                    prod.Send(message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue);
                    this.lblMsg.Text = "發送成功!!";                     
                }
            }
        }

        public void Recieve()
        {
            //創建連接工廠
            IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616");
            //通過工廠構建連接
            IConnection connection = factory.CreateConnection();
            //這個是連接的客戶端名稱標識
            connection.ClientId = "firstQueueListener";
            //啟動連接,監聽的話要主動啟動連接
            connection.Start();
            //通過連接創建一個會話
            ISession session = connection.CreateSession();
            //通過會話創建一個消費者,這里就是Queue這種會話類型的監聽參數設置
            IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("firstQueue"), "filter='demo'");
            //注冊監聽事件
            consumer.Listener += new MessageListener(consumer_Listener);
            //connection.Stop();
            //connection.Close();  
        }

        void consumer_Listener(IMessage message)
        {
            ITextMessage msg = (ITextMessage)message;
            //異步調用下,否則無法回歸主線程
            this.textBox2.Invoke(new DelegateRevMessage(RevMessage), msg);

        }

        public delegate void DelegateRevMessage(ITextMessage message);

        public void RevMessage(ITextMessage message)
        {
            this.textBox2.Text += string.Format(@"接收到:{0}{1}", message.Text, Environment.NewLine);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Recieve();
        }
    }
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM