RABBITMQ Example Tutorial: Theme Switches

Source: Internet
Author: User

In the previous example, although we used direct routing instead of fanout routing to solve the problem of blind broadcast, direct routing has its drawbacks, and he cannot route forward based on multiple criteria.


In the above log system, if not only want to make a subscription based on the log level, but also want to make a subscription based on the source of the log to do what? At this point you may think of the Syslog Service in UNIX system Tools, which is based not only on the log level (Info/warn/crit ... ) will also be routed according to the operation (Auth/cron/kern ... ) to do route forwarding.


In that case, the log system is much more flexible, not only to listen for critical errors from ' cron ', but also to listen to all the logs from ' Kern '. In fact, the subject switch (topic Exchange) can solve this problem.


  Subject switch (Topic Exchange)


The routing code for a theme switch cannot be arbitrary and must be a list of words separated by small tree points. These words can be written casually, but they are usually words related to the characteristics of the connection message. The effective route code should be such as "Stock.usd.nyse", "NYSE.VMW", "Quick.orange.rabbit". The routing code can be written casually, but the length is limited to 255 bytes.


Note that the binding code must also be in the same form. A topic switch is similar to a direct switch-a message with a specific routing code is routed to all matching binding code queues, but there are two special binding codes:


*: It can replace a word


#: It can replace 0 or more words

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/74/7C/wKiom1YekEyRndmFAABl8iEOXGg312.jpg "title=" Rabbit.png "alt=" Wkiom1yekeyrndmfaabl8ieoxgg312.jpg "/>


In this example, we send messages to all animals that correspond to three words (the first word describes the speed; the second word describes the color; the third word describes the species) the routing code that makes up the message: "<speed>.<colour>.< Species> ".

We created three bindings: Q1 uses "*.orange.*" bindings, Q2 uses "*.*.rabbit" and "lazy.#" bindings. The meanings of these bindings are as follows:

Q1 describes all animals with orange color.

Q2 describes the animals that are rabbits and the lazy animals.

In this way, the "quick.orange.rabbit" message is forwarded to the Q1, Q2 two queues through the route. The "lazy.orange.elephant" message is also forwarded to the Q1, Q2 two queues. The "Quick.orange.fox" message will only be forwarded to the Q1 queue, and "Lazy.brown.fox" will only be forwarded to the Q2 queue. "Lazy.pink.rabbit" is forwarded to the Q2 queue once, although it matches two bindings. "Quick.brown.fox" does not match any one queue will be discarded.

If we break the rules and only send one or four words at a time, such as "orange" or "quick.orange.male.rabbit", these messages do not match any bindings and will be discarded. However, if a message such as "Lazy.orange.male.rabbit" is sent, it will still be forwarded to the Q2 queue because it matches the final binding.

The theme switch is a very powerful switch, and when it binds only "#", it will receive all messages, similar to the fanout switch. When the "*" and "#" symbols are not used, the role of the subject switch is equivalent to the direct switch.


  Source


Emitlogtopic.java

package com.favccxx.favrabbit;import com.rabbitmq.client.channel;import  com.rabbitmq.client.connection;import com.rabbitmq.client.connectionfactory;public class  emitlogtopic {private static final string exchange_name =  "Topic_logs"; Public static void main (STRING[]&NBSP;ARGV)  {Connection connection = null; channel channel = null;try {connectionfactory factory = new  ConnectionFactory (); Factory.sethost ("localhost"); connection = factory.newconnection (); channel =  connection.createchannel (); Channel.exchangedeclare (exchange_name,  "topic"); string[] routingkeys = {  "Fast.orange.duck",  "Slow.orange.fish",  "Grey.rabbit",   "Fast.black.rabbit", "Quick.white.rabbit",  "Lazy.dog",  "Lazy.black.pig"  }; string[] messages = {  "Hello",  "Guys",  "Girls",  "babies" &nbsP;}; for  (int i = 0; i < routingkeys.length; i++)  {for  (int  j = 0; j < messages.length; j++)  {channel.basicpublish (EXCHANGE_ Name, routingkeys[i], null, messages[j].getbytes ("UTF-8")); System.out.println (" [x] Sent " " + routingKeys[i] + " ': ' " +  messages[j] +  "'");}}}  catch  (exception e)  {e.printstacktrace ();}  finally {if  (Connection != null)  {try {connection.close ();}  catch  (Exception ignore)  {}}}}


Receivelogstopic.java

Package com.favccxx.favrabbit;import java.io.ioexception;import com.rabbitmq.client.amqp;import  com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import  com.rabbitmq.client.connectionfactory;import com.rabbitmq.client.consumer;import  com.rabbitmq.client.defaultconsumer;import com.rabbitmq.client.envelope;public class  receivelogstopic {private static final string exchange_name =  "Topic_logs"; Public static void main (STRING[]&NBSP;ARGV)  throws exception {connectionfactory  factory = new connectionfactory (); Factory.sethost ("localhost"); Connection connection = factory.newconnection (); Channel channel = connection.createchannel (); Channel.exchangedeclare (EXCHANGE_NAME,  "topic "); String queuename = channel.queuedeclare (). Getqueue (); string[] bindingkeys = {  "*.orange.*",  " *.*.rabbit ", " lazy.# " };for  (Final string bindingkey : bindingkeys)  { Channel.queuebind (Queuename, exchange_name, bindingkey); Consumer consumer = new defaultconsumer (channel)  {@Overridepublic  void  Handledelivery (STRING&NBSP;CONSUMERTAG,&NBSP;ENVELOPE&NBSP;ENVELOPE,&NBSP;AMQP. Basicproperties properties,byte[] body)  throws IOException {String message  = new string (body,  "UTF-8"); System.out.println ("["  + bindingKey +  "]&NBSP;RECEIVED&NBSP;MESSAGE&NBSP;: '"  +  message +  "'  from routingKey : "  + envelope.getroutingkey ());}; Channel.basicconsume (Queuename, true, consumer);}}}


Run the message sender in the message receiving platform output content as follows

[*.orange.*] received message : ' Hello '  from routingkey : fast.orange.duck[* . *.rabbit] received message : ' Guys '  from routingkey : fast.orange.duck[lazy . #] received message : ' Girls '  from routingkey : fast.orange.duck[*.orange.* ] received message : ' Babies '  from routingkey : fast.orange.duck[*.*.rabbit] &NBSP;RECEIVED&NBSP;MESSAGE&NBSP: ' Hello '  from routingKey : slow.orange.fish[lazy.#]  RECEIVED&NBSP;MESSAGE&NBSP: ' Guys '  from routingKey : slow.orange.fish[*.orange.*]  RECEIVED&NBSP;MESSAGE&NBSP: ' Girls '  from routingKey : slow.orange.fish[*.*.rabbit]  RECEIVED&NBSP;MESSAGE&NBSP: ' Babies '  from routingKey : slow.orange.fish[lazy.#]  RECEIVED&NBSP;MESSAGE&NBSP: ' Hello '  from routingKey : fast.black.rabbit[*.orange.*]  Received message : ' Guys '  from routingkey : fast.black.rabbit[*.*.rabbit] received message  : ' Girls '  from routingkey : fast.black.rabbit[lazy.#] received message  : ' Babies '  from routingkey : fast.black.rabbit[*.orange.*] received message  : ' Hello '  from routingkey : quick.white.rabbit[*.*.rabbit] received message  : ' Guys '  from routingkey : quick.white.rabbit[lazy.#] received message  : ' Girls '  from routingkey : quick.white.rabbit[*.orange.*] received message &NBSP: ' Babies '  from routingKey : quick.white.rabbit[*.*.rabbit] Received  message : ' Hello '  from routingkey : lazy.dog[lazy.#] received message : ' Guys '  from routingkey : lazy.dog[*.orange.*] received message : ' Girls '  from routingKey :  lazy.dog[*.*.rabbit] received message : ' Babies '  from routingKey :  lazy.dog[lazy.#] received message : ' Hello '  from routingkey : lazy.black.pig [*.orange.*] received message : ' Guys '  from routingkey : lazy.black.pig[*.*. rabbit] received message : ' Girls '  from routingkey : lazy.black.pig[lazy.#]  received message : ' Babies '  from routingkey : lazy.black.pig


This article is from the "This person's IT World" blog, be sure to keep this source http://favccxx.blog.51cto.com/2890523/1703031

RABBITMQ Example Tutorial: Theme Switches

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.