您的位置:首页 > 编程语言 > C#

.NET/C# RabbitMQ

2016-03-25 16:42 260 查看

本系列文章均来自官网原文,属于个人翻译,如有雷同,权当个人归档,忽喷.

RabitMQ 是一个消息中间件,其实就是从消息生产者那里接受消息,然后发送给消息消费者.在这个传输过程中,可以定义一些缓存,持久化,路由的规则。

相关对象的术语简介:

1:生产者(producters)---发送消息的程序叫做生产者,使用带字母P的图来表示

View Code
运行代码,可以通过客户端管理工具看到结果.

View Code

class Receive
{
private static void Main(string[] args)
{
var factory = new ConnectionFactory() { HostName = "192.168.15.128" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);

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);
};
channel.BasicConsume(queue: "hello",
noAck: true,
consumer: consumer);

Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}

}

运行代码Send:



成功发送。

运行代码Reveive:



消息成功接收.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: