.NET使用RabbitMQ发送消息

using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class RabbitMQSender
    {
        private static RabbitMQSender instance;
        private static IConnection connection;
        private static IModel channel;
        // 交换机
        private readonly static string ExchangeName = Appsettings.app(new string[] { "RabbitMQ", "QueueBase", "Exchange" });
        // 队列
        private readonly static string QueueName = Appsettings.app(new string[] { "RabbitMQ", "QueueBase", "Queue" });


        private RabbitMQSender()
        {
            // 初始化MQ连接
            var factory = new ConnectionFactory()       // 创建连接工厂对象
            {
                HostName = Appsettings.app(new string[] { "RabbitMQ", "RabbitConnect", "HostName" }),
                Port = Appsettings.app(new string[] { "RabbitMQ", "RabbitConnect", "Port" }).ObjToInt(),
                UserName = Appsettings.app(new string[] { "RabbitMQ", "RabbitConnect", "UserName" }),
                Password = Appsettings.app(new string[] { "RabbitMQ", "RabbitConnect", "Password" }),
                VirtualHost = Appsettings.app(new string[] { "RabbitMQ", "RabbitConnect", "VirtualHost" }),
           
            }; // 设置RabbitMQ服务器地址
            connection = factory.CreateConnection();    // 创建连接对象
            channel = connection.CreateModel();         // 创建连接会话对象
            //channel.ExchangeDeclare(ExchangeName, "direct", false, false, null);
        }
        public static RabbitMQSender Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new RabbitMQSender();
                }
                return instance;
            }
        }
        public void PublishMessage(string message)
        {
            if (string.IsNullOrEmpty(message))
                return;
            try
            {
                // 消息内容
                var body = Encoding.UTF8.GetBytes(message);
                IBasicProperties basicProperties = channel.CreateBasicProperties();
                basicProperties.DeliveryMode = 2;   // 持久化
                // 发送消息
                channel.BasicPublish(exchange: ExchangeName, routingKey: QueueName, true, basicProperties: basicProperties, body: body);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"publishing message to RabbitMQ error.ErrorMessage:【{ex.Message}】,PushMessage:【{message}】");throw;
            }
        }

        public void Dispose()
        {
            if (channel != null)
            {
                channel.Close();
                channel.Dispose();
            }
            if (connection != null)
            {
                connection.Close();
                connection.Dispose();
            }
        }
    }
}

 

// 使用
var rabbitMQ = RabbitMQSender.Instance;
rabbitMQ.PublishMessage(message + i);

 

配置

 "RabbitMQ": {
    "RabbitConnect": {
      "HostName": "XX.XX.XX.XXX", // 主机名称 IP
      "Port": 5672, // 主机端口
      "VirtualHost": "/",
      "UserName": "admin", // 连接账号
      "Password": "admin" // 连接密码
    },
    "QueueBase": {
      "Exchange": "confirm.exchange", // 交换机名称
      "Queue": "confirm.queue", // 队列名称
      "ExchangeType": "direct" // 交换机类型 direct、fanout、headers、topic 必须小写
    }
  },

  

 

posted @ 2024-08-01 17:42  chocolateXLL  阅读(25)  评论(0编辑  收藏  举报