首页 > 代码库 > RabbitMQ(三)
RabbitMQ(三)
官方的使用教程(测试运行)
1、"Hello World!"
We‘re about to tell the server to deliver us the messages from the queue. Since it will push us messages asynchronously, we provide a callback. That is what EventingBasicConsumer.Received event handler does.
我们将告诉服务器从队列中发给我们消息。服务器异步地推送给我们消息,我们提供一个回调。这就是 EventingBasicConsumer.Received 事件处理程序做的事。
using RabbitMQ.Client;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Send_Simple{ class Program { static void Main(string[] args) { var factory = new ConnectionFactory() { HostName = "", UserName = "tangqun", Password = "123456" }; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null); string message = "Hello World!"; var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body); Console.WriteLine(" [x] Send {0}", message); } Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); } } }}
using RabbitMQ.Client;using RabbitMQ.Client.Events;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Receive_Simple{ class Program { static void Main(string[] args) { var factory = new ConnectionFactory() { HostName = "", UserName = "tangqun", Password = "123456" }; 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(); } } } }}
2、Work Queues
RabbitMQ(三)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。