Пример #1
0
 public void Send()
 {
     using (IConnection con = RabbitMQFactory.CreateConnection()) //创建连接对象
     {
         using (IModel channel = con.CreateModel())               //创建连接会话对象
         {
             String queueName = "queue1";
             //声明一个队列
             channel.QueueDeclare(
                 queue: queueName, //消息队列名称
                 durable: false,   //是否缓存
                 exclusive: false,
                 autoDelete: false,
                 arguments: null
                 );
             int i = 0;
             while (true)
             {
                 String message = i.ToString();
                 //消息内容
                 byte[] body = Encoding.UTF8.GetBytes(message);
                 //发送消息
                 channel.BasicPublish(exchange: "", routingKey: queueName, basicProperties: null, body: body);
                 Console.ForegroundColor = ConsoleColor.Green;
                 Console.WriteLine("成功发送消息:" + message);
                 Thread.Sleep(500);
                 i++;
             }
         }
     }
 }
Пример #2
0
 public void Receive()
 {
     using (IConnection conn = RabbitMQFactory.CreateConnection())
     {
         using (IModel channel = conn.CreateModel())
         {
             String queueName = "queue1";
             //声明一个队列
             channel.QueueDeclare(
                 queue: queueName, //消息队列名称
                 durable: false,   //是否缓存
                 exclusive: false,
                 autoDelete: false,
                 arguments: null
                 );
             //创建消费者对象
             var consumer = new EventingBasicConsumer(channel);
             consumer.Received += (model, ea) =>
             {
                 Thread.Sleep(time);       //随机等待,实现能者多劳,;//等待1秒,
                 byte[] message = ea.Body; //接收到的消息
                 Console.ForegroundColor = ConsoleColor.Red;
                 Console.WriteLine($"{name}接收到信息为:" + Encoding.UTF8.GetString(message));
                 channel.BasicAck(ea.DeliveryTag, true);
             };
             //消费者开启监听
             channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer);
             channel.BasicQos(0, 1, false);
             Console.ReadKey();
         }
     }
 }
Пример #3
0
        public void Receive()
        {
            int random = new Random().Next(1, 1000);

            using (IConnection conn = RabbitMQFactory.CreateConnection())
            {
                using (IModel channel = conn.CreateModel())
                {
                    //交换机名称
                    String exchangeName = exchange;
                    //声明交换机
                    channel.ExchangeDeclare(exchange: exchangeName, type: "direct");
                    //channel.ExchangeDeclare(exchange: exchangeName, type: "fanout");
                    //消息队列名称
                    String queueName = exchangeName + "_" + random.ToString();
                    //声明队列
                    channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
                    //将队列与交换机进行绑定
                    channel.QueueBind(queue: queueName, exchange: exchangeName, routingKey: routekey);
                    //声明为手动确认
                    channel.BasicQos(0, 1, false);
                    //定义消费者
                    var consumer = new EventingBasicConsumer(channel);
                    //接收事件
                    consumer.Received += (model, ea) =>
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        byte[] message = ea.Body;//接收到的消息
                        Console.WriteLine($"{name},{queueName}接收到信息为:" + Encoding.UTF8.GetString(message));
                        //返回消息确认
                        channel.BasicAck(ea.DeliveryTag, true);
                        Thread.Sleep(time);
                    };
                    //开启监听
                    channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer);
                    Console.ReadKey();
                }
            }
        }
Пример #4
0
 public void Send()
 {
     using (IConnection con = RabbitMQFactory.CreateConnection()) //创建连接对象
     {
         using (IModel channel = con.CreateModel())               //创建连接会话对象
         {
             channel.ExchangeDeclare(exchange: exchange, type: "direct");
             int i = 0;
             while (true)
             {
                 String message = i.ToString();
                 //消息内容
                 byte[] body = Encoding.UTF8.GetBytes(message);
                 //发送消息
                 channel.BasicPublish(exchange: exchange, routingKey: routingKey, basicProperties: null, body: body);
                 Console.ForegroundColor = ConsoleColor.Green;
                 Console.WriteLine("成功发送消息:" + message);
                 Thread.Sleep(200);
                 i++;
             }
         }
     }
 }