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

}

}

相关推荐
材料苦逼不会梦到计算机白富美2 小时前
golang分布式缓存项目 Day 1
分布式·缓存·golang
想进大厂的小王2 小时前
项目架构介绍以及Spring cloud、redis、mq 等组件的基本认识
redis·分布式·后端·spring cloud·微服务·架构
Java 第一深情2 小时前
高性能分布式缓存Redis-数据管理与性能提升之道
redis·分布式·缓存
许苑向上3 小时前
【零基础小白】 window环境下安装RabbitMQ
rabbitmq
ZHOU西口4 小时前
微服务实战系列之玩转Docker(十八)
分布式·docker·云原生·架构·数据安全·etcd·rbac
zmd-zk4 小时前
kafka+zookeeper的搭建
大数据·分布式·zookeeper·中间件·kafka
yx9o9 小时前
Kafka 源码 KRaft 模式本地运行
分布式·kafka
Gemini199510 小时前
分布式和微服务的区别
分布式·微服务·架构
G丶AEOM10 小时前
分布式——BASE理论
java·分布式·八股
P.H. Infinity16 小时前
【RabbitMQ】03-交换机
分布式·rabbitmq