您的位置:首页 > 博客中心 > 电脑问题 >

RabbitMQ---1、c#实现

时间:2022-03-18 10:35

Producter 发送消息代码:

  1.   /// <summary>
  2.   /// 连接配置
  3.   /// </summary>
  4.   private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory(){
  5.   HostName ="192.168.1.8",UserName="hao",Password="abc123",Port= 5672
  6.   };
  7.   /// <summary>
  8.   /// 路由名称
  9.   /// </summary>
  10.   const string TopExchangeName = "topic.justin.exchange";
  11.    
  12.   //队列名称
  13.   const string TopQueueName = "topic.justin.queue";
  14.    
  15.   public static void TopicExchangeSendMsg()
  16.   {
  17.   using (IConnection conn = rabbitMqFactory.CreateConnection())
  18.   {
  19.   using (IModel channel = conn.CreateModel())
  20.   {
  21.   channel.ExchangeDeclare(TopExchangeName, "topic", durable: false, autoDelete: false, arguments: null);
  22.   channel.QueueDeclare(TopQueueName, durable: false, autoDelete: false, exclusive: false, arguments: null);
  23.   channel.QueueBind(TopQueueName, TopExchangeName, routingKey: TopQueueName);
  24.   //var props = channel.CreateBasicProperties();
  25.   //props.Persistent = true;
  26.   string vadata = Console.ReadLine();
  27.   while (vadata != "exit")
  28.   {
  29.   var msgBody = Encoding.UTF8.GetBytes(vadata);
  30.   channel.BasicPublish(exchange: TopExchangeName, routingKey: TopQueueName, basicProperties: null, body: msgBody);
  31.   Console.WriteLine(string.Format("***发送时间:{0},发送完成,输入exit退出消息发送", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
  32.   vadata = Console.ReadLine();
  33.   }
  34.   }
  35.   }
  36.   }

Customer接收消息代码:

  1.   /// <summary>
  2.   /// 连接配置
  3.   /// </summary>
  4.   private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory() {
  5.   HostName = "192.168.1.8", UserName = "hao", Password = "abc123", Port = 5672
  6.   };
  7.    
  8.   /// <summary>
  9.   /// 路由名称
  10.   /// </summary>
  11.   const string TopExchangeName = "topic.justin.exchange";
  12.    
  13.   //队列名称
  14.   const string TopQueueName = "topic.justin.queue";
  15.    
  16.   public static void TopicAcceptExchange()
  17.   {
  18.   using (IConnection conn = rabbitMqFactory.CreateConnection())
  19.   {
  20.   using (IModel channel = conn.CreateModel())
  21.   {
  22.   channel.ExchangeDeclare(TopExchangeName, "topic", durable: false, autoDelete: false, arguments: null);
  23.   channel.QueueDeclare(TopQueueName, durable: false, autoDelete: false, exclusive: false, arguments: null);
  24.   channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
  25.   channel.QueueBind(TopQueueName, TopExchangeName, routingKey: TopQueueName);
  26.   var consumer = new EventingBasicConsumer(channel);
  27.   consumer.Received += (model, ea) =>
  28.   {
  29.   var msgBody = Encoding.UTF8.GetString(ea.Body);
  30.   Console.WriteLine(string.Format("***接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody));
  31.   int dots = msgBody.Split(‘.‘).Length - 1;
  32.   System.Threading.Thread.Sleep(dots * 1000);
  33.   Console.WriteLine(" [x] Done");
  34.   channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
  35.   };
  36.   channel.BasicConsume(TopQueueName, noAck: false, consumer: consumer);
  37.    
  38.   Console.WriteLine("按任意值,退出程序");
  39.   Console.ReadKey();
  40.   }
  41.   }
  42.   }

 

热门排行

今日推荐

热门手游