///
/// RabbitMQ帮助类
///
public class RabbitMQHelper
{
IConnection connection = null;
IModel channel = null;
public RabbitMQHelper(string rabbitMQConnection)
{
connection = new ConnectionFactory() { Uri = new Uri(rabbitMQConnection) }.CreateConnection();
channel = connection.CreateModel();
}
public RabbitMQHelper()
{
connection = new ConnectionFactory() { Uri = new Uri(RabbitMQConfig.RabbitMQConnection) }.CreateConnection();
channel = connection.CreateModel();
}
///
/// 发送消息
///
/// 交换机名称
/// 路由key
/// 队列名称
/// 消息内容
/// 交换机类型,默认:ExchangeType.Direct
public void Send(string exchangeName, string routingKey, string queueName, string content, string exchangeType=ExchangeType.Direct)
{
if (string.IsNullOrEmpty(content)) return;
channel.ExchangeDeclare(exchangeName, exchangeType, true, false, null);
channel.QueueDeclare(queueName, true, false, false, null);
channel.QueueBind(queueName, exchangeName, routingKey, null);
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
properties.DeliveryMode = 1;
byte[] body = Encoding.UTF8.GetBytes(content);
//开始发送
channel.BasicPublish(exchangeName, routingKey, properties, body);
}
///
/// 发送消息
///
/// 交换机名称
/// 路由key
/// 队列名称
/// 消息内容集合
/// 交换机类型,默认:ExchangeType.Direct
public void Send(string exchangeName, string routingKey, string queueName, List contents, string exchangeType = ExchangeType.Direct)
{
if (contents == null || contents.Count == 0) return;
channel.ExchangeDeclare(exchangeName, exchangeType, true, false, null);
channel.QueueDeclare(queueName, true, false, false, null);
channel.QueueBind(queueName, exchangeName, routingKey, null);
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
properties.DeliveryMode = 1;
foreach(var content in contents)
{
byte[] body = Encoding.UTF8.GetBytes(content);
//开始发送
channel.BasicPublish(exchangeName, routingKey, properties, body);
}
}
///
/// 创建消费者接收消息
///
/// 交换机名称
/// 路由key
/// 队列名称
/// 回调函数
/// 交换机类型,默认:ExchangeType.Direct
/// 错误回调函数
public void CreateConsumer(string exchangeName, string routingKey, string queueName, Action callback, string exchangeType = ExchangeType.Direct, Action errorCallback=null)
{
Task.Run(() =>
{
channel.ExchangeDeclare(exchangeName, exchangeType, true, false, null);
channel.QueueDeclare(queueName, true, false, false, null);
channel.QueueBind(queueName, exchangeName, routingKey, null);
channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
ReadOnlyMemory body = ea.Body;
var message = Encoding.UTF8.GetString(body.ToArray());
try
{
callback(message);
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
}
catch(Exception ex)
{
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
errorCallback?.Invoke();
return;
}
};
channel.BasicConsume(queueName, autoAck: false, consumer: consumer);
});
}
}
RabbitMQ 帮助类,大家可以nuget:UtilsSharp.RabbitMQ 进行引用
全局配置下就可以用了:
RabbitMQConfig.RabbitMQConnection="xxx.xxx.xxx.xx";