RABBITMQ Message Queue (iv): Distribute to multiple consumer (publish/subscribe) [Go]

Source: Internet
Author: User

In the previous article, we put each message as deliver (provided) to a consumer. In this article, we will deliver the same message (provided) to multiple consumer. This pattern is also known as "Publish/subscribe".
In this article, we will create a log system that contains two parts: the first part is issue log (Producer), the second part is received and printed (Consumer). We'll build two consumer, the first one to write the log to the physical disk, and the second to output the log to the screen.

1. Exchanges

The concept of Exchange isdescribed in more detail in the RABBITMQ Message Queuing (i): detailed Introduction detailed introduction. Now make a brief review.

The messaging model of RabbitMQ is that producer does not send a message directly to the queue. In fact, producer does not know whether the message it sends has reached the queue.

The message that producer sends is actually sent to exchange. The functionality of Exchange is also simple: receive a message from producer and then post it to the queue. Does exchange need to know how to handle a message, put it in a queue, or put it in more than one queue? This rule is defined by the type of Exchange.

We know there are three types of exchange:Direct, topic and fanout. Fanout is broadcast mode, and all of the message is placed in the queue it knows. Create an exchange with the name logs, type Fanout.
Channel. Exchangedeclare ("logs""fanout");
2. Temporary queues

As of now, we use a queue that has a name: the first is Hello, the second is task_queue. Using a named queue makes it possible to share a queue before producer and consumer.

But for the log system we're going to build, we don't need a queue with a name. We want to get all the logs, not the middle part of them. And we're only interested in the current log. To achieve this goal, we need two things:
1) Whenever consumer is connected, we need a new, empty queue. Because we are not interested in old log. Fortunately, if you don't specify a name when declaring a queue, RABBITMQ randomly chooses that name for us. It's basically like this:AMQ.GEN-JZTY20BRGKO-HJMUJJ0WLG.

2) When consumer closes the connection, the queue is deleted. You can add a exclusive parameter. Method: NET does not seem to set exclusive, consumer will automatically delete queue after closing.

null);
3. Bindings bindings Now we have created the Fanout type of exchange and the non-name queue (in fact RABBITMQ has given us a name). How does Exchange know which queue the message is sent to? The answer is through bindings: binding.

Method:

Take name:

Channel. Exchangedeclare (Exchange_name, "fanout");//Broadcast
Queuedeclareok Queueok = Channel. Queuedeclare ();
varQueueok. QueueName;

Binding:

Channel. Queuebind (QueueName, Exchange_name, Routing_key); // You do not need to specify the routing key, set the Fanout, the point is not used.

Now logs exchange attaches its message to the queue we created.

The complete fanout example:Producer.cs
1 Static voidMain (string[] args)2         {3             varFactory =NewConnectionFactory () {HostName ="localhost" };4             using(varConnection =Factory. CreateConnection ())5             {6                 using(varChannel =connection. Createmodel ())7                 {8                     Const stringExchange_name ="logs";9                     Const stringRouting_key ="";TenChannel.Exchangedeclare(Exchange_name,"fanout");//Broadcast One                     varMessage =GetMessage (args); A                     varBODY =Encoding.UTF8.GetBytes (message); -Channel. Basicpublish (Exchange_name, Routing_key,NULL, body);//You do not need to specify the routing key, set the Fanout, the point is not used. -Console.WriteLine ("[x] Sent {0}", message); the                 } -             } -}

Consumer.cs

1 Static voidMain (string[] args)2         {3             varFactory =NewConnectionFactory () {HostName ="localhost" };4             using(varConnection =Factory. CreateConnection ())5             {6                 using(varChannel =connection. Createmodel ())7                 {8                     Const stringExchange_name ="logs";9                     Const stringRouting_key ="";TenChannel. Exchangedeclare (Exchange_name,"fanout");//Broadcast OneQueuedeclareok Queueok = Channel. Queuedeclare ();//whenever consumer is connected, we need a new, empty queue. Because we are not interested in old log. Fortunately, if you don't specify a name when declaring a queue, RABBITMQ randomly chooses that name for us.  A                     ////Now we have created the Fanout type of exchange and the non-name queue (in fact RABBITMQ has given us a name).  -                     ////How does Exchange know which queue the message is sent to? The answer is through bindings: binding.  -                     stringQueueName = Queueok.queuename;//got RABBITMQ to give us a name. theChannel.Queuebind(QueueName, Exchange_name, Routing_key);//You do not need to specify the routing key, set the Fanout, the point is not used. -                     varConsumer =NewQueueingbasicconsumer (channel); -Channel. Basicconsume (QueueName,true, consumer); -Console.WriteLine ("[*] waiting for messages."+"To exit Press CTRL + C"); +                      while(true) -                     { +                         varea = (Basicdelivereventargs) consumer. Queue.dequeue ();//pending Operations A                         varBODY =ea. Body; at                         varMessage =Encoding.UTF8.GetString (body); -Console.WriteLine ("[x] Received {0}", message); -                     } -                 } -             } -}

Turn:

Http://www.rabbitmq.com/tutorials/tutorial-three-dotnet.html (official website)

http://blog.csdn.net/anzhsoft/article/details/19617305 (translation)

RABBITMQ Message Queue (iv): Distribute to multiple consumer (publish/subscribe) [Go]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.