RabbitMQ学习之HelloWorld(1)

 

RabbitMQ就是一个消息代理(message broker),能够用来接收和发送消息。html

消息队列有一些黑话,咱们来看下:python

  • Producer : 发送message的程序
  • Queue : 能够用来存储message
  • Consumer : 接收message的程序

注意,producer 和 consumer 和 queue 能够在同一台主机,也能够不在同一台主机。一般不在api

Hello World

如图,P表示producer , C 表示consumer . 中间的盒子表示queue异步

Sending

 

发送message的producer函数

Send.csspa

using System;using RabbitMQ.Client;using System.Text;
class Send
{
    public static void Main()
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using(var connection = factory.CreateConnection()) //创建链接,若是咱们想链接到一个不一样机器的broker,咱们能够指定一个名字或者ip
        using(var channel = connection.CreateModel())  //创建通道,大多数api获取数据都在这里
        {
            channel.QueueDeclare(queue: "hello",  //声明一个queue(用来把message发送过去)
                                 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] Sent {0}", message);
        }

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

若是你第一次使用RabbitMQ,而且发送message失败。那么有多是the broker(代理:指RabbitMQ 或者说queue)启动的时候没有足够的硬盘空间(默认须要至少50M)所以拒绝接受请求。代理

你能够检查the broker 的日志文件来确认而且减小限制。详细 configuration file documentation 日志

Receiving 

对于consumer ,它用来从RabbitMQ监听message.因此,它会持续监听message.code

Receive.cshtm

using RabbitMQ.Client;using RabbitMQ.Client.Events;using System;using System.Text;
class Receive
{
    public static void Main()
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using(var connection = factory.CreateConnection())  //创建链接
        using(var channel = connection.CreateModel())  //创建通道
        {
            channel.QueueDeclare(queue: "hello",  //声明queue
                                 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",  //接受消息动做
                                 autoAck: true,
                                 consumer: consumer);

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

注意,咱们在这里也声明了queue.由于咱们可能在启动publisher以前先启动consumer,因此咱们在consume messages以前须要确保queue存在。

咱们在接收queue中的message时是异步的,因此咱们提供了一个回调函数。即 EventingBasicConsumer.Received

实际效果及代码改进

代码结构以下

本示例直接在一个解决方案中创建了两个控制台程序,用来send和receive 队列queue中的message.

代码使用上面讲述的代码,

效果以下:

上面的代码直接在程序启动后就发送了message.使看起来不是很直观,下面是对send.cs修改后的代码。

send.cs

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

                
                while (true) { var message = Console.ReadLine(); //string message = "Hello World!";
                    var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body); Console.WriteLine(" [x] Sent {0}", message); //Console.WriteLine(" Press [exit] to exit.");
 }
            }

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

 如上代码,使用while循环稍做修改,是能够持续手动输入message

效果以下

 

如上,能够看出,当想要持续发送时,咱们在发送端加了while循环。

可是,接收端却不须要作任何修改,由于接收端自己是在持续监听queue里的message。

参考网址:RabbitMQ

相关文章
相关标签/搜索