.NET 组件连接 activemq

组件安装

复制代码
Install-Package Apache.NMS.ActiveMQ

.NET 组件连接 activemq

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using Apache.NMS.ActiveMQ.Commands;
using Apache.NMS.Util;
using Microsoft.Extensions.Configuration;

namespace GraphqlApp.BackendAPI.Services
{
    public class MqService
    {
        private string _mqUrl = string.Empty;
        private string _Q_Biz1 = string.Empty;


        public string Q_Biz1
        {
            get {
                return _Q_Biz1;
            }
        }

        public MqService(IConfiguration configuration)
        {
            _mqUrl = configuration.GetValue<string>("Mq:MqUrl");

            _Q_Biz1 = configuration.GetValue<string>("Mq:Q_Biz1");
        }

        private IConnection CreateConnection()
        {

            Uri _uri = new Uri(String.Concat($"activemq:failover:({_mqUrl})"));
            IConnectionFactory factory = new ConnectionFactory(_uri);

            var result = factory.CreateConnection();
            result.AcknowledgementMode = AcknowledgementMode.ClientAcknowledge;
            return result;
        }

        public void Product(string queuesName, string msg)
        {
            Task.Run(() =>
            {
                using (IConnection _conn = CreateConnection())
                {

                    using (Apache.NMS.ISession _session = _conn.CreateSession())
                    {
                        IDestination _destination = SessionUtil.GetDestination(_session, queuesName);
                        using (IMessageProducer producer = _session.CreateProducer(_destination))
                        {
                            ITextMessage request = _session.CreateTextMessage(msg);
                            producer.Send(request);
                        }
                    }
                }
            });
        }

        public void Consume(string queuesName, Func<string, int> func)
        {
            Task.Run(() =>
            {
                using (IConnection conn = this.CreateConnection())
                {   
                    using (ISession session = conn.CreateSession(AcknowledgementMode.ClientAcknowledge))
                    {   
                        conn.Start();
                        IDestination destination = SessionUtil.GetDestination(session, queuesName);
                        using (IMessageConsumer consumer = session.CreateConsumer(destination))
                        {
                            consumer.Listener += (IMessage message) =>
                            {
                                ITextMessage msg = (ITextMessage)message;
                                Console.WriteLine("从MQ接收到消息:" + msg.Text);

                                var funcResult = func(msg.Text);
                                if (funcResult > 0)
                                    msg.Acknowledge();

                            };
                            Console.ReadLine();
                        }
                    }
                }
            });

            //Console.ReadLine();

        }




    }
}
相关推荐
我是唐青枫9 小时前
C#.NET 范围与索引(Range、Index)完全解析:语法、用法与最佳实践
c#·.net
深海潜水员11 小时前
【MonoGame游戏开发】| 牧场物语实现 第一卷 : 农场基础实现 (下)
vscode·游戏·c#·.net·monogame
时光追逐者1 天前
Visual Studio 2026 现已正式发布,更快、更智能!
ide·c#·.net·visual studio
用户7227868123442 天前
.NET 实现雪花算法:高效生成分布式唯一 ID
.net
玩泥巴的2 天前
.NET 8+ 飞书API实战:自动化群组管理与消息推送
c#·.net·二次开发·飞书
唐青枫3 天前
C#.NET 范围与索引(Range、Index)完全解析:语法、用法与最佳实践
c#·.net
许泽宇的技术分享3 天前
当AI Agent遇上.NET:微软Agent Framework的架构奥秘与实战启示
人工智能·microsoft·.net
SEO-狼术3 天前
DevExpress DXperience Crack
.net
我是唐青枫3 天前
一文理解 C#.NET Tuples:从基础到高级应用
c#·.net
缺点内向4 天前
C# 中 Word 文档目录的插入与删除指南
开发语言·c#·word·.net