RABBITMQ Message Queuing (v): Routing message routing [go]

Source: Internet
Author: User
Tags rabbitmq

In the previous article, we built a simple log system. Next, we'll enrich it: the ability to use different severity (severity) to listen for different levels of log. For example, we hope that only the error log will be saved to disk.

1. Bindings Bindings

In the previous article we did this by binding:

Channel. Queuebind (QueueName, Exchange_name, Routing_key);//const string routing_key = "";

The binding is actually associated with Exchange and queue. Or so: The queue is interested in the content of Exchagne, and exchange has to deliver its message into the queue.

In fact, the binding can take routing key as a parameter. In fact, the name of this parameter and the basic_publish parameter name are the same.

To avoid confusion, we refer to this routing key as the binding key (that is, routing key in Exchange)

Use a binding key to create the binding:

Channel. Queuebind (QueueName, Exchange_name, Routingkey);//string routingkey = "Name of the specified Routingkey";

In the previous article we talked about using fanout type of exchange, which is ignored for fanout exchange.

2. Direct Exchange

The routing algorithm for Direct exchange is very simple: it can be explained by the exact match of the binding key.


Exchange X and the two queue are bound together. Q1 's binding key is orange. Q2 's binding key is black and green.
When P releases the key that is orange, exchange puts it on Q1. If P releases a key that is black or green then it will go to Q2. The rest of the message will be discarded. 3. Multiple bindings multiple queue bindings the same key is possible. For example, Q1 and Q2 are bound to black. That is, for routing key is Black's message, it will be deliver (provide) to Q1 and Q2. The rest of the message will be discarded.
4. Emitting logs

First, we're going to create a direct exchange:

Const string " Direct_logs "  "direct");

We will use log's severity (severity) as the routing key, so that consumer can handle different types of logs for different severity (severity levels).

null, body);

We use three kinds of severity (severity level): ' Info ', ' warning ', ' error '.

5. Subscribing

For queue, we need to bind severity (severity level):

Const string " Direct_logs "  "direct"); string queuename = Channel. Queuedeclare (); channel. Queuebind (QueueName, Exchange_name, Routingkey);
6. Final versionThis example does not specify the name of the queue:Producer. CS
1 ///SendDemo5.exe Info2         ///SendDemo5.exe Warning3         ///SendDemo5.exe Error4         /// </param>5         Static voidMain (string[] args)6         {7             varFactory =NewConnectionFactory () {HostName ="localhost" };8             using(varConnection =Factory. CreateConnection ())9             {Ten                 using(varChannel =connection. Createmodel ()) One                 { A                     Const stringExchange_name ="Direct_logs"; -Channel. Exchangedeclare (Exchange_name, "direct" );//Direct: If routing key matches, then the message is passed to the corresponding queue.  -Exchangewhen the queue is created, it automatically binds that exchange with the name of the queue as routing key.  the                     var Routingkey= (args. Length >0) ? args[0] :"Info"; -  -                     varMessage = (args. Length >1) ?string. Join (" ", args. Skip (1). ToArray ()):"Hello world!"; -                     varBODY =Encoding.UTF8.GetBytes (message); +                      -Channel. Basicpublish (Exchange_name,Routingkey,NULL, body); +Console.WriteLine ("[x] Sent ' {0} ': ' {1} '", Routingkey, message); A                 } at             } -}

Consumer.cs

1 /// <summary>2         ///Receive side create temporary queue3         /// </summary>4         /// <param name= "args" >5         ///ReceiveDemo5.exe Info6         ///ReceiveDemo5.exe Warning7         ///ReceiveDemo5.exe Error8         /// </param>9         Static voidMain (string[] args)Ten         { One             varFactory =NewConnectionFactory () {HostName ="localhost" }; A             using(varConnection =Factory. CreateConnection ()) -             { -                 using(varChannel =connection. Createmodel ()) the                 { -                     Const stringExchange_name ="Direct_logs"; -Channel. Exchangedeclare (Exchange_name, "direct" ); -                     stringQueueName =Channel. Queuedeclare ();//Get the name of the queue +                     if(args. Length <1) -                     { +Console.Error.WriteLine ("Usage: {0} [info] [WARNING] [ERROR]", Environment.getcommandlineargs () [0]); AEnvironment.exitcode =1; at                         return; -                     } -  -                     foreach(varRoutingkeyinchargs) -                     { - Channel. Queuebind (QueueName, Exchange_name, routingkey); in                     } -  toConsole.WriteLine ("[*] waiting for messages."+"To exit Press CTRL + C"); +  -                     varConsumer =NewQueueingbasicconsumer (channel); theChannel. Basicconsume (QueueName,true, consumer); *  $                      while(true)Panax Notoginseng                     { -                         varEA =(Basicdelivereventargs) consumer. Queue.dequeue (); the  +                         varBODY =ea. Body; A                         varMessage =Encoding.UTF8.GetString (body); the                         varRoutingkey =ea. Routingkey; +Console.WriteLine ("[x] Received ' {0} ': ' {1} '", - routingkey, message); $                     } $                 } -             } -}

You must run consumer first, and then run Producer.

ReceiveDemo5.exe Info Warning Error or ReceiveDemo5.exe info,receivedemo5.exe warning,receivedemo5.exe error

SendDemo5.exe Info wyp,senddemo5.exe warning Wyp,senddemo5.exe warning WYP

Ext.: http://www.rabbitmq.com/tutorials/tutorial-four-dotnet.html (official website) http://blog.csdn.net/anzhsoft/article/details /19630147 (translation)

RABBITMQ Message Queuing (v): Routing message routing [go]

Related Article

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.