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

}

}

相关推荐
processflow流程图34 分钟前
分布式kettle调度平台v6.4.0新功能介绍
分布式
全栈开发圈43 分钟前
干货分享|分布式数据科学工具 Xorbits 的使用
分布式
运维&陈同学3 小时前
【zookeeper01】消息队列与微服务之zookeeper工作原理
运维·分布式·微服务·zookeeper·云原生·架构·消息队列
时差9533 小时前
Flink Standalone集群模式安装部署
大数据·分布式·flink·部署
菠萝咕噜肉i3 小时前
超详细:Redis分布式锁
数据库·redis·分布式·缓存·分布式锁
只因在人海中多看了你一眼7 小时前
分布式缓存 + 数据存储 + 消息队列知识体系
分布式·缓存
zhixingheyi_tian9 小时前
Spark 之 Aggregate
大数据·分布式·spark
求积分不加C11 小时前
-bash: ./kafka-topics.sh: No such file or directory--解决方案
分布式·kafka
nathan052911 小时前
javaer快速上手kafka
分布式·kafka
谭震鸿14 小时前
Zookeeper集群搭建Centos环境下
分布式·zookeeper·centos