RabbitMQ 链接管理-发布者-消费者

RabbitMQ连接管理器

using RabbitMQ.Client;

using System;

public class RabbitMQConnectionManager

{

private readonly IConnectionFactory _connectionFactory;

private IConnection _connection;

public RabbitMQConnectionManager(string hostName)

{

_connectionFactory = new ConnectionFactory() { HostName = hostName };

}

public IModel CreateModel()

{

if (_connection == null || !_connection.IsOpen)

{

_connection = _connectionFactory.CreateConnection();

}

return _connection.CreateModel();

}

public void CloseConnection()

{

if (_connection != null && _connection.IsOpen)

{

_connection.Close();

}

}

}

RabbitMQ发布者

using RabbitMQ.Client;

using System.Text;

public class RabbitMQPublisher

{

private readonly RabbitMQConnectionManager _connectionManager;

private readonly string _queueName;

public RabbitMQPublisher(RabbitMQConnectionManager connectionManager, string queueName)

{

_connectionManager = connectionManager;

_queueName = queueName;

}

public void Publish(string message)

{

using (var channel = _connectionManager.CreateModel())

{

channel.QueueDeclare(queue: _queueName,

durable: false,

exclusive: false,

autoDelete: false,

arguments: null);

var body = Encoding.UTF8.GetBytes(message);

channel.BasicPublish(exchange: "",

routingKey: _queueName,

basicProperties: null,

body: body);

}

}

}

RabbitMQ消费者

using RabbitMQ.Client;

using RabbitMQ.Client.Events;

using System;

using System.Text;

public class RabbitMQConsumer

{

private readonly RabbitMQConnectionManager _connectionManager;

private readonly string _queueName;

public RabbitMQConsumer(RabbitMQConnectionManager connectionManager, string queueName)

{

_connectionManager = connectionManager;

_queueName = queueName;

}

public void Consume()

{

var channel = _connectionManager.CreateModel();

channel.QueueDeclare(queue: _queueName,

durable: false,

exclusive: false,

autoDelete: false,

arguments: null);

var consumer = new EventingBasicConsumer(channel);

consumer.Received += (model, ea) =>

{

var body = ea.Body.ToArray();

var message = Encoding.UTF8.GetString(body);

Console.WriteLine(" [x] Received {0}", message);

};

channel.BasicConsume(queue: _queueName,

autoAck: true,

consumer: consumer);

}

}

使用

class Program

{

static void Main(string[] args)

{

var connectionManager = new RabbitMQConnectionManager("localhost");

var queueName = "hello";

var consumer = new RabbitMQConsumer(connectionManager, queueName);

consumer.Consume();

var publisher = new RabbitMQPublisher(connectionManager, queueName);

publisher.Publish("Hello World!");

Console.WriteLine(" Press [enter] to exit.");

Console.ReadLine();

connectionManager.CloseConnection();

}

}

相关推荐
千层冷面1 小时前
RabbitMQ 发送者确认机制详解
分布式·rabbitmq·ruby
ChinaRainbowSea1 小时前
3. RabbitMQ 的(Hello World) 和 RabbitMQ 的(Work Queues)工作队列
java·分布式·后端·rabbitmq·ruby·java-rabbitmq
敖正炀1 小时前
基于RocketMQ的可靠消息最终一致性分布式事务解决方案
分布式
一條狗3 小时前
随笔 20250402 分布式 ID 生成器 Snowflake 里面的坑
分布式
小马爱打代码3 小时前
Kubernetes 中部署 Ceph,构建高可用分布式存储服务
分布式·ceph·kubernetes
码熔burning3 小时前
【Spring Cloud Alibaba】:Nacos 入门讲解
分布式·spring cloud·微服务
hycccccch11 小时前
Canal+RabbitMQ实现MySQL数据增量同步
java·数据库·后端·rabbitmq
低头不见16 小时前
一个服务器算分布式吗,分布式需要几个服务器
运维·服务器·分布式
靠近彗星17 小时前
如何检查 HBase Master 是否已完成初始化?| 详细排查指南
大数据·数据库·分布式·hbase
陈平安Java and C18 小时前
RabbitMQ简单介绍和安装
rabbitmq