ASP.NET ActiveMQ 消息队列
1.引入
2.发送消息
3.接收消息
概述:MQ消息存放在内存,重启后,消息丢失。接收后,消息丢失(只取一次),不取,一直在且速度快。
使用前:下载apache-activemq-5.15.2-bin ,下载完运行activemq.bat(必须提前安装Java sdk)
注册成功后: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(); } } }
天生我材必有用,千金散尽还复来
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)