Article 6 of the rabbitmq series: Exchange of the topic type

Source: Internet
Author: User
Tags key string rabbitmq

In the previous blog, we made some improvements to the previous log system. Instead of fanout exchange, direct exchange is used to selectively receive log messages.

Although direct exchange is used to improve the log system, there are still some restrictions (messages cannot be routed based on multiple factors ).

In our log system, we hope to subscribe based not only on the log level, but also on the specified routing key. As you can understand, such as UNIX System Log tools, log message routing rules are not only based on log levels (info/warn/crit ...), It can also be based on the device (AUTH/cron/Kern ...).

This greatly improves the flexibility. For example, we can only listen to the error-level logs pushed by Kern.

To implement this function in our logging system, we need to know more about topic-type exchange.

1. Exchange of the topic type

When a message is sent to a topic-type exchange, the routing_key cannot be specified at Will (it must be a string of a series of words connected by periods. Words can be arbitrary, but it is generally associated with messages more or less ). The length of the routing key cannot exceed 255 bytes.

The binding key must also be in the same way. Exchange of the topic type is like a direct exchange: a message specified by the consumer to determine the routing key will be pushed to all consumers whose binding key can match it. However, such binding has two special cases:

  • * (Asterisk): can replace a word (a string of consecutive tokens)
  • # (Well number): can match one or more characters

The following is an example:

In this example, we will send messages describing animals. The first word of the routing key describes the speed, the second word describes the color, and the third word describes the species: "<speed>. <color>. <Species> ".

Here we create three binding: Q1 with binding key as "*. Orange. *", and Q2 with binding key as "*. *. Rabbit" and "lazy.

These bindings can be summarized:

  • Q1 interested in all orange animals;
  • Q2 hopes to get information about all rabbits and lazy. # animals.

A line with "quick. orange. A message with rabbit as the routing key will be pushed to the queue Q1 and Q2, and the routing key is "lazy. orange. elephant messages will also be pushed to Q1 and Q2. However, if the routing key is "quick. orange. the message will only be pushed to Q1. The routing key is "lazy. brown. the message of Fox will be pushed to Q2, and the routing key is "lazy. pink. rabbit messages will also be pushed to Q2, but the same message will only be pushed to Q2 once.

If the specified exchange and routing key are not bound to the exchange and binding key at the consumer end, the message will be discarded. For example, "orange" and "quick. Orange. Male. Rabbit ". However, messages whose routing is "lazy. Orange. Male. Rabbit" will be pushed to Q2.

TopicType exchange:

Exchange of the topic type is very powerful, and other types of exchange can be implemented.

  • When a queue is bound with a binding key of "#", it receives all messages, which is similar to fanout exchange.
  • When binding key does not contain "*" and "#", it is similar to direct type exchange.
2. Final Implementation

We are going to use topic-type exchange in the log system. In the beginning, we prepare routing keys to use two words: "<Facility>. <severity> ". The code is similar to that in the previous blog. emitlogtopic. Java:

Public class emitlogtopic {Private Static final string exchange_name = "topic_logs"; public static void main (string [] argv) throws exception {connectionfactory factory = new connectionfactory (); factory. sethost ("localhost"); connection = factory. newconnection (); channel = connection. createchannel (); // specifies the exchange channel of the topic type. exchangedeclare (exchange_name, "topic"); // The routing key string routingkey = getrouting (argv); string message = getmessage (argv); channel. basicpublish (exchange_name, routingkey, null, message. getbytes (); system. out. println ("[x] sent '" + routingkey + "': '" + message + "'"); connection. close ();}//...}

Receivelogstopic. Java code:

Public class extends elogstopic {Private Static final string exchange_name = "topic_logs"; public static void main (string [] argv) throws exception {connectionfactory factory = new connectionfactory (); factory. sethost ("localhost"); connection = factory. newconnection (); channel = connection. createchannel (); // specifies the exchange channel of the topic type. exchangedeclare (exchange_name, "topic"); string queuename = channel. queuedeclare (). getqueue (); If (argv. length <1) {system. err. println ("Usage: cancelogstopic [binding_key]... "); system. exit (1);} // bind binding key for (string bindingkey: argv) {channel. queuebind (queuename, exchange_name, bindingkey);} system. out. println ("[*] waiting for messages. to exit press Ctrl + C "); queueingconsumer consumer = new queueingconsumer (Channel); channel. basicconsume (queuename, true, consumer); While (true) {queueingconsumer. delivery delivery = consumer. nextdelivery (); string message = new string (delivery. getbody (); string routingkey = delivery. getenvelope (). getroutingkey (); system. out. println ("[x] received'" + routingkey + "':'" + message + "'");}}}

The running status is as follows:

3. Summary

Based on the above, the routing key and binding key are enriched.

 

Reference: http://www.rabbitmq.com/tutorials/tutorial-five-java.html

Article 6 of the rabbitmq series: Exchange of the topic type

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.