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

        }




    }
}
相关推荐
WineMonk7 小时前
.NET WPF CommunityToolkit.Mvvm框架
.net·wpf·mvvm
界面开发小八哥7 小时前
界面控件DevExpress WPF中文教程:Data Grid——卡片视图设置
.net·wpf·界面控件·devexpress·ui开发
九鼎科技-Leo18 小时前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
九鼎科技-Leo20 小时前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
.net开发20 小时前
WPF怎么通过RestSharp向后端发请求
前端·c#·.net·wpf
九鼎科技-Leo1 天前
在 C# 中,ICollection 和 IList 接口有什么区别?
windows·c#·.net
时光追逐者1 天前
C#/.NET/.NET Core学习路线集合,学习不迷路!
开发语言·学习·c#·asp.net·.net·.netcore·微软技术
Crazy Struggle1 天前
.NET 全功能流媒体管理控制接口平台
.net·开源项目·流媒体
.net开发1 天前
WPF使用prism框架发布订阅实现消息提示
c#·.net·wpf