Exemplo n.º 1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="message"></param>
 /// <param name="exchange"></param>
 /// <param name="queue"></param>
 /// <param name="routingKey"></param>
 public static void PublishToQueue(string publishName, string message, string queue = "", string exchange = "", string routingKey = "")
 {
     if (string.IsNullOrEmpty(publishName))
     {
         return;
     }
     try
     {
         RabbitMQInstance instance = null;
         if (publishName == ConstValues.Admin)
         {
             instance = adminMQ;
         }
         else
         {
             instance = RabbitMQInstance.Instance(publishName).FirstOrDefault();
         }
         if (instance == null)
         {
             return;
         }
         if (string.IsNullOrEmpty(exchange))
         {
             exchange = instance.RabbitMQModel.Exchange;
         }
         if (string.IsNullOrEmpty(queue))
         {
             queue = instance.RabbitMQModel.Queue;
         }
         if (string.IsNullOrEmpty(routingKey))
         {
             routingKey = instance.RabbitMQModel.RoutingKey;
         }
         var channel = instance.Channel;
         channel.ExchangeDeclare(exchange, ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
         channel.QueueDeclare(queue, durable: true, exclusive: false, autoDelete: false, arguments: null);
         channel.QueueBind(queue, exchange, routingKey, null);
         var body       = Encoding.UTF8.GetBytes(message);
         var properties = channel.CreateBasicProperties();
         properties.Persistent = true;//持久化
         channel.BasicPublish(exchange: exchange, routingKey: routingKey, basicProperties: properties, body: body);
         Console.WriteLine("Send Message:" + message);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
        private static void InitRabbitMQ(RabbitMQInstance rabbitMQInstance)
        {
            if (rabbitMQInstance == null)
            {
                throw new Exception("rabbitMQInstance Is Null");
            }
            _connection = rabbitMQInstance.Factory.CreateConnection();
            _channel    = _connection.CreateModel();
            var sExchange   = rabbitMQInstance.RabbitMQModel?.Exchange;
            var sQueue      = rabbitMQInstance.RabbitMQModel?.Queue;
            var sRoutingKey = rabbitMQInstance.RabbitMQModel?.RoutingKey;

            _channel.ExchangeDeclare(sExchange, ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
            _channel.QueueDeclare(sQueue, durable: true, exclusive: false, autoDelete: false, arguments: null);
            _channel.QueueBind(sQueue, sExchange, sRoutingKey, null);
            _channel.BasicQos(0, prefetch, false);
            _connection.ConnectionShutdown += RabbitMQ_ConnectionShutdown;
        }
Exemplo n.º 3
0
        public static void PublishToAdmin(string publishName, string message, string exchange = "", string queue = "", string routingKey = "")
        {
            if (string.IsNullOrEmpty(publishName) || string.IsNullOrEmpty(message))
            {
                return;
            }
            RabbitMQInstance instance = null;

            if (publishName == ConstValues.Admin)
            {
                instance = adminMQ;
            }
            else
            {
                instance = RabbitMQInstance.Instance(publishName).FirstOrDefault();
            }
            if (instance == null)
            {
                return;
            }
            PublishToQueue(ConstValues.Admin, publishName + ConstValues.PublishNameSepareter + message);
        }