该段代码异常简单,几乎到了无法精简的地步:创建了一个信道(channel)->创建一个队列->向该队列发送消息。
2、Consumer端:
- var factory = new ConnectionFactory() { HostName = "localhost" };
- using (var connection = factory.CreateConnection())
- {
- using (var channel = connection.CreateModel())
- {
- //创建一个名为"hello"的队列,防止producer端没有创建该队列
- channel.QueueDeclare(queue: "hello",
- durable: false,
- exclusive: false,
- autoDelete: false,
- arguments: null);
- //回调,当consumer收到消息后会履行该函数
- var consumer = new EventingBasicConsumer(channel);
- consumer.Received += (model, ea) =>
- {
- var body = ea.Body;
- var message = Encoding.UTF8.GetString(body);
- Console.WriteLine(" [x] Received {0}", message);
- };
- //花费队列"hello"中的消息
- channel.BasicConsume(queue: "hello",
- noAck: true,
- consumer: consumer);
- Console.WriteLine(" Press [enter] to exit."
推荐阅读
论文:Feature Pyramid Networks for Object Detection论文地址:https://arxiv.org/abs/1612.03144这里介绍的文┞仿是来自 Facebook 的特点金字塔收集 Feature Pyramid Networks(FPN)。F>>>详细阅读
本文标题:如何优雅的使用RabbitMQ
地址:http://www.17bianji.com/lsqh/36412.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示