首页 > 代码库 > 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();            }        }    }}
Send_Simple
技术分享
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();                }            }        }    }}
Receive_Simple

 

2、Work Queues

 

RabbitMQ(三)