In the previous article, we implemented the flexible configuration of the message system. Instead of using a fanout switch configuration. Use direct switches and have the ability to selectively receive messages after routing keys.
Although using direct-connect switches can improve our system, it still has limitations and cannot implement multiple conditions routing.
In our messaging system, we want to subscribe not only to a routing key-based queue, but also to a production message-based source. These concepts come from Unix tool syslog. This log is based on strict (info/warn/crit...) and easy (auth/cron/kern...) routing methods. Our example is simpler than this one.
This example will give us a lot of flexibility, for example, we want to listen to both the 'cron' error logs and all the logs from 'kern'.
To achieve this flexibility, we need to know more about topic switches.
Theme switch
When using a topic switch, you cannot use arbitrary routing keys. The format of the routing key should be words divided by points. Any word you use can usually express the meaning. For example "stock.usd.nyse", "nyse.vmw", "quick.orange.rabbit". But the word size is limited to a maximum of 255 bytes.
Use the theme switch to define routing keys to pay attention to 2 points
Define routing keys that match the topic switch
In this example, we will send all messages describing the animal. This message will be sent together with a routing key composed of 3 words and 2 points. The first word is to express speed, the second describes color, and the third describes type: "<speed>.<colour>.<species>".
Create three types of bindings, Q1 and the key "*.orange.*" binding, Q2 and "*.*.rabbit" and "lazy.#" binding.
An overview of the three binding relationships is:
A message with the routing key set to "quick.orange.rabbit" will be passed to the Q1 and Q2 queues. The same goes for "lazy.orange.elephant" too. "quick.orange.fox" will go to the first queue, "lazy.brown.fox" will go to the second queue, and "lazy.pink.rabbit" will go to the second queue in time. It matches 2 bindings. "quick.brown.fox" will be discarded because of mismatch.
So what about "orange" and "quick.orange.male.rabbit"? Because no binding is matched, it will be discarded.
So what about the routing keys of "lazy.orange.male.rabbit"? , since lazy.# matches this will be passed to the second queue.
Tips for theme exchangers
Theme switch is awesome and acts similar to other switches.
Code Example
The code is no different from the previous routing code, please see
Config.java
package com.zb.rabbitMQtest.t5topics.config;import org.springframework.amqp.core.*;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * @author Zhang Bo*/@Configuration(value = "t5Config")public class Config { /** * Created by: Zhang Bo* Time: 2018/3/5 10:45 am * @apiNote Define topic exchange*/ @Bean public TopicExchange topicExchange() { return new TopicExchange("topic-exchange"); } /** * Creator: Zhang Bo* Time: 2018/3/5 10:48 am * @apiNote Define automatic deletion of anonymous queue*/ @Bean public Queue autoDeleteQueue0() { return new AnonymousQueue(); } /** * Creator: Zhang Bo* Time: 2018/3/5 10:48 am * @apiNote Define automatic deletion of anonymous queue*/ @Bean public Queue autoDeleteQueue1() { return new AnonymousQueue(); } /** * Created by: Zhang Bo* Time: 2018/3/5 10:48 am * @param topicExchange Theme Switch* @param autoDeleteQueue0 Automatically delete the queue* @apiNote Binding Use the routing key orange to queue to the topic switch* @return Binding */ @Bean public Binding binding0a(TopicExchange topicExchange, Queue autoDeleteQueue0) { return BindingBuilder.bind(autoDeleteQueue0).to(topicExchange).with("*.orange.*"); } /** * Created by: Zhang Bo* Time: 2018/3/5 10:48 am * @param topicExchange Theme Switch* @param autoDeleteQueue1 Automatically delete the queue* @apiNote Binding using the autoDeleteQueue1 with the routing key black to the topic switch* @return Binding */ @Bean public Binding binding1a(TopicExchange topicExchange, Queue autoDeleteQueue1) { return BindingBuilder.bind(autoDeleteQueue1).to(topicExchange).with("*.*.rabbit"); } /** * Created by: Zhang Bo* Time: 2018/3/5 10:48 am * @param topicExchange Theme Switch* @param autoDeleteQueue1 Automatically delete the queue* @apiNote Binding Use the routing key green to queue to the topic switch* @return Binding */ @Bean public Binding binding1b(TopicExchange topicExchange, Queue autoDeleteQueue1) { return BindingBuilder.bind(autoDeleteQueue1).to(topicExchange).with("lazy.#"); }} Receiver.java
package com.zb.rabbitMQtest.t5topics.receiver;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;/** * @author Zhang Bo*/@Component(value = "t5Receiver")public class Receiver { @RabbitListener(queues = "#{autoDeleteQueue0.name}") public void receiver0(String str) { System.out.println("receiver0++++++++++:" + str); //try { // Thread.sleep(1000); //} catch (InterruptedException e) { // e.printStackTrace(); //} } @RabbitListener(queues = "#{autoDeleteQueue1.name}") public void receiver1(String str) { System.out.println("receiver1++++++++++:" + str); //try { // Thread.sleep(1000); //} catch (InterruptedException e) { // e.printStackTrace(); //} }} Send.java
package com.zb.rabbitMQtest.t5topics.send;import org.springframework.amqp.core.TopicExchange;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;/** * @author Zhang Bo*/@Component(value = "t5Send")public class Send { @Autowired private TopicExchange topicExchange; @Autowired private RabbitTemplate rabbitTemplate; private String[] keys = {"quick.orange.rabbit", "lazy.orange.elephant", "quick.orange.fox", "lazy.brown.fox", "lazy.pink.rabbit", "quick.brown.fox"}; public void send() { String message = "hahaha"; for (int i = 0; i < 5; i++) { System.out.println("send++++++++++++:".concat(message)); rabbitTemplate.convertAndSend(topicExchange.getName(), keys[5], message); } }} SendTest.java
package com.zb.rabbitMQtest.t5topics.send;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;/** * @author Zhang Bo*/@RunWith(SpringRunner.class)@SpringBootTestpublic class SendTest { @Autowired private Send send; @Test public void send() throws Exception { send.send(); }}I won't leave the test results, please check it yourself.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.