当前位置:网站首页>DirectExchange switch simple introduction demo
DirectExchange switch simple introduction demo
2022-07-31 07:09:00 【Max Heng】
The source code is at the end of the article
Pre-learning knowledge
1. FanoutExchange switch code tutorial SpringAMQP Simple Getting Started demo2
Students who know Fanout switches must know that when sending messages to Fanout switches, you only need to specify the name of the switch and the message to be sent, and you can specify multiple queues FanoutExchange switch code tutorial
@Testpublic void testSendFanoutExchange() {// switch nameString exchangeName = "Declare FanoutExchange";// informationString message = "Test the FanoutExchange switch, please accept it!";rabbitTemplate.convertAndSend(exchangeName, "", message);}The difference between the Direct switches we want to learn below is very similar to Fanout, But the only difference is that each Queue sets a BindingKey with Exchange, And you also need to specify the RoutingKey of the message when publishing the message
Let's demonstrate with code
@RabbitListener(bindings = @QueueBinding(//queue namevalue = @Queue("direct.queue1"),// switch nameexchange = @Exchange(value = "direct", type = ExchangeTypes.DIRECT),//keykey = {"red", "blue"}))public void listenDirectQueue1(String msg){System.out.println("The consumer received the message of fanout.queue1: [" + msg + "]");}@RabbitListener(bindings = @QueueBinding(value = @Queue("direct.queue2"),exchange = @Exchange(value = "direct"),key = {"red", "yellow"}))public void listenDirectQueue2(String msg){System.out.println("The consumer received the message of fanout.queue1: [" + msg + "]");}Case 1 The value of key is blue
@Testpublic void testSendDirectExchange() {// switch nameString exchangeName = "direct";// informationString red = "hello, blue!";rabbitTemplate.convertAndSend(exchangeName, "blue", red);}Run result:

Parse:
The reason is very simple "blue" is included in the key declared in our QueueBinding

Case 2 The key value is red
@Testpublic void testSendDirectExchange() {// switch nameString exchangeName = "direct";// informationString red = "hello, red!";rabbitTemplate.convertAndSend(exchangeName, "red", red);}Running result:
Analysis:
We can see that this time the message was received by two queues at the same time. In the queues direct.queue1 and direct.queue2, the keys declared in QueueBinding also contain"red"
Source code project address:
Link: https://pan.baidu.com/s/1vnYVEPGO8B5XLCf9Xc67cA Extraction code: few9
边栏推荐
猜你喜欢
随机推荐
Debian 10 iptables (防火墙)配置
自动翻译软件-批量批量自动翻译软件推荐
Project exercise - memorandum (add, delete, modify, check)
js原型详解
es6数组/数组对象求并集、交集、差集、去重、排序
Dart入门
数据库原理作业2 — JMU
防抖和节流
Bulk free text translation
拓扑排序的两种方法 --- dfs+Kahn
2022.7.29 数组
Oracle入门 02 - IT软硬件平台及操作系统介绍
Oracle入门 11 - Linux 开关机及系统进程命令
Shell编程规范与变量
零样本学习&Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning
Oracle 11g R2 与 Linux 版本的选择
线程唤醒机制
Database Principles Homework 2 — JMU
Install the gstreamer development dependency library to the project sysroot directory
Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model









