Esempio n. 1
0
        public static void BuildProductionTest(MQHeard heard, string data)
        {
            string key = heard.Config.HostUrl + "&" + heard.Config.Port + "&" + heard.Config.VirtualHost;

            if (mapConnection[key].IsOpen == false)
            {
                Console.WriteLine("连接已关闭,重新打开");
                CreateConnection(heard.Config);
            }

            Console.WriteLine("准备发送 " + data);
            var sendBytes = Encoding.UTF8.GetBytes(data);

            using (var channel = mapConnection[key].CreateModel())
            {
                channel.ExchangeDeclare(heard.Info.ExchangeName, heard.Info.ExchangeType); //声明交换机
                channel.QueueDeclare(heard.Info.QueueName, false, false, false, null);     //声明一个队列
                channel.QueueBind(heard.Info.QueueName, heard.Info.ExchangeName, heard.Info.RouteKey);
                channel.ConfirmSelect();
                channel.BasicPublish(heard.Info.ExchangeName, heard.Info.RouteKey, null, sendBytes);
                if (channel.WaitForConfirms() == false)
                {
                    Console.Write("Confirm失败");
                }
            }
        }
Esempio n. 2
0
 public static void BuildProductionConfirms(MQHeard heard, string[] datas)
 {
     try
     {
         string key = heard.Config.HostUrl + "&" + heard.Config.Port + "&" + heard.Config.VirtualHost;
         if (mapChannel[key].IsClosed)
         {
             Console.WriteLine("连接已关闭,重新打开");
             CreateConnection(heard.Config, false);
         }
         mapChannel[key].ConfirmSelect();
         foreach (string data in datas)
         {
             var sendBytes = Encoding.UTF8.GetBytes(data);
             mapChannel[key].BasicPublish(heard.Info.ExchangeName, heard.Info.RouteKey, null, sendBytes);
         }
         if (mapChannel[key].WaitForConfirms() == false)
         {
             Console.Write("Confirm失败");
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("推mq消息异常");
     }
 }
Esempio n. 3
0
        public static void BuildConsume(MQHeard heard, Action <string> action)
        {
            string key = heard.Config.HostUrl + "&" + heard.Config.Port + "&" + heard.Config.VirtualHost;

            if (mapConnection.ContainsKey(key) == false)
            {
                return;
            }
            IModel _channel = mapChannel[key];

            _channel.ExchangeDeclare(heard.Info.ExchangeName, heard.Info.ExchangeType); //声明交换机
            _channel.QueueDeclare(heard.Info.QueueName, false, false, false, null);     //声明一个队列
            _channel.QueueBind(heard.Info.QueueName, heard.Info.ExchangeName, heard.Info.RouteKey);
            EventingBasicConsumer consumer = new EventingBasicConsumer(_channel);       //事件基本消费者

            consumer.Received += (ch, ea) =>
            {
                var context = (BasicDeliverEventArgs)ea;
                var data    = Encoding.UTF8.GetString(context.Body.ToArray());
                action(JsonConvert.SerializeObject(data));
                _channel.BasicAck(ea.DeliveryTag, false);                 //确认该消息已被消费
            };
            _channel.BasicConsume(heard.Info.QueueName, false, consumer); //启动消费者 设置为手动应答消息
        }