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();
        }
    }
}

天生我材必有用,千金散尽还复来
原文地址:https://www.cnblogs.com/ligenyun/p/7731942.html