• .NET 组件连接 activemq


    组件安装

    Install-Package Apache.NMS.ActiveMQ
    
    • 1

    .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("Mq:MqUrl");
    
                _Q_Biz1 = configuration.GetValue("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 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();
    
            }
    
    
    
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
  • 相关阅读:
    websocket php教程
    【腾讯云 Cloud Studio 实战训练营】通过云IDE构建Web3项目
    元梦之星内测上线,如何在B站打响声量?
    SpringBoot-调用外部接口(三种方式)
    视频转换芯片MS7200概述 HDMI转数字RGB/YUV/HDMI RXReceive/替代IT66021FN
    基于单片机预费电表控制系统(proteus仿真+源程序)
    第17章 触发器
    安装 TypeScript 并编译成JS
    JAVA成员变量首字母小写,第二个字母大写报错问题(原因:Lombok与Spring冲突)
    海信电视U8“死磕”技术,家庭影音娱乐的体验突围
  • 原文地址:https://blog.csdn.net/daijinmingcn/article/details/133178398