RABBITMQ Introduction (iv)--TOPIC exchanger

Source: Internet
Author: User
Tags rabbitmq

Today we are going to learn the last exchanger type: topic. Direct is placed in a queue in exchange bindings, and fanout is placed in all the queues in exchange bindings. Is there any part of the queue that is placed in the exchange bindings, or if multiple routing keys can be routed to a queue, then the topic type of exchange will be used.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/EA/wKiom1cMrYvCk0vYAAETr3j13dU392.png "title=" 1.PNG " alt= "Wkiom1cmryvck0vyaaetr3j13du392.png"/>


Let's start by looking at how multiple routing keys are routed to a queue. Let's say we have three systems, write logs when they go wrong, and send logs to RABBITMQ, with the routing key: System name. Error. In the rabbitmq we want to put all the error information into a queue, you can use the following way:

package com.jaeger.exchange.topic;import java.io.ioexception;import  Java.util.concurrent.timeoutexception;import org.junit.test;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 producer  {private static final String MY_EXCHANGE_NAME =  "Myexchange";//  Three routing keys used when sending logs from different systems private static final string sys1_error_routing_key =  " Sys1.error ";p rivate static final string sys2_error_routing_key = " Sys2.error "; private static final string sys3_error_routing_key =  "Sys3.error";p rivate  static final string my_queue_name =  "Myqueue";p Rivate static final string topic =  "TOPIC";p rivate static final string host =   "172.19.64.21";p rivate static final string user =  "Jaeger";p rivate  static final string password =  "Root";p rivate static final int  port = 5672; @Testpublic  void createexchangeandqueue ()  throws IOException,  Timeoutexception {connectionfactory connectionfactory = new connectionfactory (); Connectionfactory.sethost (HOST); Connectionfactory.setusername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport (PORT); Connection connection = connectionfactory.newconnection (); Channel channel = connection.createchannel ();//  Create a TOPIC type of Exchangechannel.exchangedeclare (my_exchange_name, topic);//  Create a Queuechannel.queuedeclare (my_queue_name, false, false, false, null);// Build a Routing key, bind Exchange and queue together, but here the Routing key is not a//  specific name, but it can match all the routing that end with. Error.  keychannel.queuebind (my_queue_name, my_exchange_name,  "*.error"); Channel.close (); Connection.close ();} @Testpublic  void produce ()  throws ioexception, timeoutexception { Connectionfactory connectionfactory = new connectionfactory (); ConnectionFactory.setHost ( HOST); Connectionfactory.setusername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport ( PORT); Connection connection = connectionfactory.newconnection (); Channel channel = connection.createchannel (); string message =  "hello  world!"; * Send a message to RABBITMQ. We specify the names of exchange and the 3 different routing key here, and RABBITMQ will find exchange that has the name, and if it finds this exchange is the topic type, Will try to match the Routing key of the exchange bindings with the specified Routing key, and the queue that matches the Routing key will receive the message. */channel.basicpublish (my_exchange_name, sys1_error_routing_key, nUll, message.getbytes ("Utf-8")); Channel.basicpublish (my_exchange_name, sys2_error_routing_key,  Null, message.getbytes ("Utf-8")); Channel.basicpublish (My_exchange_name, sys3_error_routing_key,  null, message.getbytes ("Utf-8")); System.out.println ("sent "  + message +  "'"); Channel.close (); Connection.close ();} @Testpublic  void consume ()  throws IOException, TimeoutException,  Interruptedexception{connectionfactory connectionfactory = new connectionfactory (); Connectionfactory.sethost (HOST); Connectionfactory.setusername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport (PORT); Connection connection = connectionfactory.newconnection (); Channel channel = connection.createchannel (); Consumer consumer = new defaultconsumer (channel)  {@Overridepublic  void  Handledelivery (string consumertag, envelope  Envelope, amqp. Basicproperties properties,byte[] body)  throws IOException {String message  = new string (body,  "UTF-8"); System.out.println ("received "  + message +  "'");}; Channel.basicconsume (My_queue_name, true, consumer); Thread.Sleep (1000);}}

Run Createexchangeandqueue and discover that Exchange has bound a *.error routing key:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/E7/wKioL1cMsP6zKwIaAAC1Y5JCoE8729.png "style=" float: none; "title=" 2.PNG "alt=" Wkiol1cmsp6zkwiaaac1y5jcoe8729.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7E/EA/wKiom1cMsEnD7U7SAABfgoOsCzQ536.png "style=" float: none; "title=" 3.PNG "alt=" Wkiom1cmsend7u7saabfgoosczq536.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/EA/wKiom1cMsErhGH85AABdPAU5gJ4835.png "style=" float: none; "title=" 4.PNG "alt=" Wkiom1cmserhgh85aabdpau5gj4835.png "/>

Then run the produce method to send a message to RABBITMQ:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/E7/wKioL1cMsX_T3jwTAABg2QnyMHI258.png "title=" 5.PNG " alt= "Wkiol1cmsx_t3jwtaabg2qnymhi258.png"/> can see 3 messages into the same queue. Finally run consume to consume the message:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/EA/wKiom1cMsQrS_393AAAo5KhmHCM069.png "style=" float: none; "title=" 6.PNG "alt=" Wkiom1cmsqrs_393aaao5khmhcm069.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7E/EA/wKiom1cMsQqytIeEAABfzA1ILfI391.png "style=" float: none; "title=" 7.PNG "alt=" Wkiom1cmsqqytieeaabfza1ilfi391.png "/>



Above we show how to get multiple routing keys routed to the same queue. Is there a way to route a routing key to multiple queues? In fact, with the topic type of exchange is completely possible. For example, our system will generate Error_levelx.log logs based on different error levels, and we'll start by keeping all of the errors in a total queue in the background, and then storing them in separate queue at level. Let's change the code above:

package com.jaeger.exchange.topic;import java.io.ioexception;import  Java.util.concurrent.timeoutexception;import org.junit.test;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 producer  {private static final String MY_EXCHANGE =  "Myexchange";p rivate  static final string sys_error_routing_key =  "Error.level1.log";p rivate static  final String SYS_ERROR_QUEUE =  "Errorqueue";p rivate static final  string sys_level1_error_queue =  "Level1errorqueue";p rivate static final string  SYS_LEVEL2_ERROR_QUEUE =  "Level2errorqueue";p rivate static final  string topic =  "TOPIC";p rivate static final string host =  " 172.19.64.21 ";p rivate static final string user = " Jaeger ";p rivate static  final String PASSWORD =  "Root";p Rivate static final int port  = 5672; @Testpublic  void createexchangeandqueue ()  throws IOException,  Timeoutexception {connectionfactory connectionfactory = new connectionfactory (); Connectionfactory.sethost (HOST); Connectionfactory.setusername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport (PORT); Connection connection = connectionfactory.newconnection (); Channel channel = connection.createchannel ();//  Create a TOPIC type of Exchangechannel.exchangedeclare (my_exchange, topic);//  Create three Queuechannel.queuedeclare (Sys_error_queue, false, false, false, null) to store the ERROR; Channel.queuEdeclare (Sys_level1_error_queue, false, false, false, null); Channel.queueDeclare (SYS_ Level2_error_queue, false, false, false, null);//  Create three Routing key, Binds EXCHANGE and three QUEUE to a channel.queuebind (sys_error_queue, my_exchange,  "Error.*.log"); Channel.queuebind (sys_level1_error_queue, my_exchange,  "Error.level1.log"); Channel.queuebind (SYS_ level2_error_queue, my_exchange,  "Error.level2.log"); Channel.close (); Connection.close ();} @Testpublic  void produce ()  throws ioexception, timeoutexception { Connectionfactory connectionfactory = new connectionfactory (); ConnectionFactory.setHost ( HOST); Connectionfactory.setusername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport ( PORT); Connection connection = connectionfactory.newconnection (); Channel channel = connection.createchannel (); string message =  "hello  World!"; * * Send a message to RABBITMQ. We specify exchange and a routing key name here, RABBITMQ will look for exchange that does not have this name, if found and found that Exchange is the topic type, it will try to use the specified routing  key to match the exchange-bound Routing key, the queue that matches the Routing key will receive a message. */channel.basicpublish (My_exchange, sys_error_routing_key, null, message.getbytes ("Utf-8")); System.out.println ("sent "  + message +  "'"); Channel.close (); Connection.close ();} @Testpublic  void consume ()  throws IOException, TimeoutException,  Interruptedexception{connectionfactory connectionfactory = new connectionfactory (); Connectionfactory.sethost (HOST); Connectionfactory.setusername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport (PORT); Connection connection = connectionfactory.newconnection (); Channel channel = connection.createchannel (); Consumer consumer = new defaultconsumer (channel)  {@Overridepublic  void  HandledelIvery (STRING CONSUMERTAG, ENVELOPE ENVELOPE, AMQP. Basicproperties properties,byte[] body)  throws IOException {String message  = new string (body,  "UTF-8"); System.out.println ("received "  + message +  "'");}; Channel.basicconsume (Sys_error_queue, true, consumer); Thread.Sleep (1000);}}

To run the Createexchangeandqueue method, create 3 queue:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/E8/wKioL1cMwPSw52DhAACxXZIAGc4442.png "style=" float: none; "title=" 1.PNG "alt=" Wkiol1cmwpsw52dhaacxxziagc4442.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7E/E8/wKioL1cMwPSjrus4AACF_-RpQY4143.png "style=" float: none; "title=" 2.PNG "alt=" Wkiol1cmwpsjrus4aacf_-rpqy4143.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/EB/wKiom1cMwECRYiDdAABgHpGWzTo348.png "style=" float: none; "title=" 3.PNG "alt=" Wkiom1cmwecryiddaabghpgwzto348.png "/>

Then run the produce method to send a message to RABBITMQ. You can see that the message only entered the Errorqueue and level1errorqueue two queues:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/E8/wKioL1cMwWCyxgERAAB9bMWsGxk722.png "title=" 4.PNG " alt= "Wkiol1cmwwcyxgeraab9bmwsgxk722.png"/> Last run consume to consume messages in the Errorqueue queue:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7E/E8/wKioL1cMwYPw5L3aAAB8XlqXFlc526.png "title=" 6.PNG " alt= "Wkiol1cmwypw5l3aaab8xlqxflc526.png"/>


So far, all of the 3 classes of Exchange RABBITMQ commonly used are covered. One thing to note about topic is its matching rules. The matching rules for topic are based on identifiers, separated by. For example, error, Level1, and log are identifiers in Error.level1.log.

* Only one identifier can be matched, such as Error.*.log can only match error.level1.log or error.high.log, but not error.log or error.level1.high.log.

#可以匹配0个或多个标识符, such as Error.#.log, can match Error.log or error.level1.high.log.


This article is from the "Bronze Gong" blog, please be sure to keep this source http://jaeger.blog.51cto.com/11064196/1763091

RABBITMQ Introduction (iv)--TOPIC exchanger

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.