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

        }




    }
}
相关推荐
鱼是一只鱼啊7 小时前
.netframeworke4.6.2升级.net8问题处理
开发语言·.net·.net8
one99612 小时前
.net 项目引用与 .NET Framework 项目引用之间的区别和相同
c#·.net·wpf
CHHC188017 小时前
ML.NET 图像分类
.net·图像分类·mlnet
步、步、为营1 天前
.net无运行时发布原理
linux·服务器·.net
zzlyx991 天前
2025年国产化推进.NET跨平台应用框架推荐
.net
张3蜂1 天前
.NET 8 项目 Docker 方式部署到 Linux 系统详细操作步骤
linux·docker·.net
CodeCraft Studio1 天前
【实用技能】如何利用条码控件Aspose.BarCode,控制 Barcode-39 中的宽窄比
.net
步、步、为营1 天前
C# 通用缓存类开发:开启高效编程之门
缓存·c#·.net