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

        }




    }
}
相关推荐
数据的世界0111 小时前
.NET开发人员学习书籍推荐
学习·.net
paixiaoxin13 小时前
CV-OCR经典论文解读|An Empirical Study of Scaling Law for OCR/OCR 缩放定律的实证研究
人工智能·深度学习·机器学习·生成对抗网络·计算机视觉·ocr·.net
19004321 小时前
.NET重点
.net
m0_6632340121 小时前
在 .NET 5.0 运行 .NET 8.0 教程:使用 ASP.NET Core 创建 Web API
前端·asp.net·.net
小码编匠1 天前
.NET 下 RabbitMQ 队列、死信队列、延时队列及小应用
后端·c#·.net
真想骂*2 天前
vmime.net_4.dll详解:它是什么,有何用途?
.net
lzhdim2 天前
2、C#基于.net framework的应用开发实战编程 - 设计(二、二) - 编程手把手系列文章...
开发语言·c#·.net
softshow10262 天前
Solon 集成 activemq-client
java·activemq·java-activemq
组合缺一2 天前
solon 集成 activemq-client (sdk)
java·solon·activemq