c# ActiveMQ

要下载 ActiveMQ 5.15.4 版本,可按照以下步骤操作:

访问官方下载页面:

打开浏览器,访问 ActiveMQ 官方下载存档页面

解压缩pache-activemq-5.15.4-bin.zip,然后双击pache-activemq-5.15.4-bin\bin\win32\activemq.bat运行ActiveMQ程序。

启动ActiveMQ以后,可以使用浏览器登陆:http://localhost:8161/admin/验证, 默认用户名是:admin 密码是:admin

(前提是配置好好JDK环境)

引入dll ,这两个dll的版本都是1.8

Apache.NMS.ActiveMQ.dll

Apache.NMS.dll

csharp 复制代码
using System;
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using Apache.NMS.ActiveMQ.Commands;

class Program
{
    static void Main(string[] args)
    {
     string userName = "adman";
    string Password = "123456";
        // 连接工厂,创建到ActiveMQ的连接
        ConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616");
        factory.UserName = userName;
        factory.Password = Password;
        using (IConnection connection = factory.CreateConnection())
        using (ISession session = connection.CreateSession())
        {
            connection.Start();
            
            // 创建队列目的地
            IDestination destination = session.GetQueue("TestQueue");
            
            // 创建消息生产者
            using (IMessageProducer producer = session.CreateProducer(destination))
            {
                // 创建文本消息
                ITextMessage message = session.CreateTextMessage("Hello ActiveMQ from C#!");
                
                // 发送消息
                producer.Send(message);
                Console.WriteLine("消息发送成功: " + message.Text);
            }
            
            // 创建消息消费者
            using (IMessageConsumer consumer = session.CreateConsumer(destination))
            {
                // 同步接收消息(等待10秒)
                IMessage receivedMessage = consumer.Receive(TimeSpan.FromSeconds(10));
                
                if (receivedMessage is ITextMessage textMessage)
                {
                    Console.WriteLine("收到消息: " + textMessage.Text);
                }
                else
                {
                    Console.WriteLine("未收到消息或消息类型不正确");
                }
            }
        }
    }
}

在ActiveMQ的C#客户端中,alwaysSessionAsync的默认值为true

参考连接:
https://cloud.tencent.com/developer/article/2163298
(activemq设置用户名密码,提高安全访问)

https://blog.csdn.net/u010316188/article/details/86771100[添加链接描述](https://blog.csdn.net/u010316188/article/details/86771100)

相关推荐
mudtools20 小时前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的1 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21881 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理1 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
lindexi1 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
feifeigo1231 天前
matlab画图工具
开发语言·matlab
dustcell.1 天前
haproxy七层代理
java·开发语言·前端
norlan_jame1 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone1 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054961 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django