This is the 14th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge
Related articles
MyBatis series summary: MyBatis series
preface
-
For example, if the log types we want to receive are info.base and info.Advantage, and a queue only wants messages from info.base, then direct will not work.
-
At this point, only the topic type can be used.
-
The routing_key of a message sent to a topic switch cannot be written arbitrarily and must meet certain requirements. It must be a list of words separated by a dot.
-
These words can be any word, such as “stock.usd. Nyse “, “NYx.vmw “, “quick.orange.rabbit”. This type.
-
Of course, this list of words cannot exceed 255 bytes.
-
List of rules
- * (asterisk) can replace a word
- # (hash sign) can replace zero or more words
I. Producers
-
/** * this is a test producer *@author DingYongJun *@date 2021/8/6 */ public class DyProducerTest_topic {private static final String EXCHANGE_NAME = "topic_logs"; /** * @param args */ public static void main(String[] args) throws Exception { publishMessageIndividually(); } public static void publishMessageIndividually () throws the Exception {/ / use the utility class to create Channel Channel Channel = RabbitMqUtils.getChannel(); /** * Q1--> bind to * orange with 3 words in the middle (*.orange.*) * Q2--> bind to * the last word is rabbit with 3 words (*.*.rabbit) * the first word is lazy #) * */ Map<String, String> bindingKeyMap = new HashMap<>(); Bindingkeymap. put(" quick.orange.Rabbit ", "received by queue Q1Q2 "); Bindingkeymap.put ("lazy.orange.elephant", "received by queue Q1Q2 "); Bindingkeymap. put("quick.orange.fox", "received by queue Q1 "); Bindingkeymap. put("lazy.brown.fox", "received by queue Q2 "); Bindingkeymap. put("lazy.pink.rabbit", "only received once by Q2 even though both bindings are satisfied "); Bindingkeymap.put ("quick.brown.fox", "not matching any binding will be received by any queue and discarded "); Bindingkeymap.put ("quick.orange.male. Rabbit ", "the four words that do not match any bindings will be discarded "); Bindingkeymap.put ("lazy.orange.male. Rabbit ", "is four words but matches Q2"); for (Map.Entry<String, String> bindingKeyEntry: bindingKeyMap.entrySet()){ String bindingKey = bindingKeyEntry.getKey(); String message = bindingKeyEntry.getValue(); channel.basicPublish(EXCHANGE_NAME,bindingKey, null, message.getBytes("UTF-8")); System.out.println(" producer sends message "+ message); }}}Copy the code
Ii. Consumers
-
A
-
/** * this is a test consumer ** @author DingYongJun * @date 2021/8/6 */ public class DyConsumerTest_topic01 {private static final String EXCHANGE_NAME = "topic_logs"; public static void main(String[] args) throws Exception { Channel channel = RabbitMqUtils.getChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String queueName="Q1"; String queueName="Q1"; channel.queueDeclare(queueName, false, false, false, null); channel.queueBind(queueName, EXCHANGE_NAME, "*.orange.*"); System.out.println(" wait to receive message.....") ); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); System.out.println(" receive queue :"+queueName+" bind key :"+ delivery.getenvelope ().getroutingKey ()+", message :"+message); }; channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { }); }}Copy the code
-
-
B
-
/** * This is a test consumer *@author DingYongJun *@date 2021/8/1 */ public class DyConsumerTest_topic02 {private static final String EXCHANGE_NAME = "topic_logs"; public static void main(String[] args) throws Exception{ Channel channel = RabbitMqUtils.getChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String queueName="Q2"; String queueName="Q2"; channel.queueDeclare(queueName, false, false, false, null); channel.queueBind(queueName, EXCHANGE_NAME, "*.*.rabbit"); channel.queueBind(queueName, EXCHANGE_NAME, "lazy.#"); System.out.println(" wait to receive message.....") ); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); System.out.println(" receive queue :"+queueName+" bind key :"+ delivery.getenvelope ().getroutingKey ()+", message :"+message); }; channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { }); }}Copy the code
-
-
The execution result
- producers
- Consumers A
- Consumers B
Third, summary
- When a queue binding key is #, then the queue will receive all data, somewhat like fanout.
- If there is no queue binding key
#
and*
So the binding type of this thing is direct. - So topic is the most flexible model! Add an asynchronous reply. It is also the most used mode in our work!
I see no ending, but I will search high and low
If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah