当前位置:网站首页>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
边栏推荐
- DirectExchange交换机简单入门demo
- 讲解实例+详细介绍@Resource与@Autowired注解的区别(全网最全)
- LeetCode brush # 376 # Medium - swing sequence
- 【并发编程】ReentrantLock的lock()方法源码分析
- 2.(1)栈的链式存储、链栈的操作(图解、注释、代码)
- 浅析伪类和伪元素
- Oracle入门 12 - Linux 磁盘分区及LVM实战
- Oracle入门 05 - VirtualBox的虚拟机安装配置
- Postgresql source code learning (34) - transaction log ⑩ - full page write mechanism
- nohup原理
猜你喜欢
随机推荐
高并发与多线程之间的难点对比(容易混淆)
在级联选择器,根据不会重复的字段,来获取当前的对象
如何在uni-app中选择一个合适的UI组件库
安装和使用uView
In-depth analysis of z-index
文本三剑客之e`grep,seq文本编辑工具
Basic usage of Koa framework
Zotero | Zotero translator插件更新 | 解决百度学术文献无法获取问题
Oracle 日期函数相关
【编程题】【Scratch三级】2022.03 冬天下雪了
DDL+DML+DQL
Third-party library-store
Some derivation formulas for machine learning backpropagation
Oracle入门 03 - Linux / Unix 系统基础知识
codec2 BlockPool:unreadable libraries
浅析重复线性渐变repeating-linear-gradient如何使用
03-SDRAM:写操作(突发)
磁盘和储存管理
Oracle入门 02 - IT软硬件平台及操作系统介绍
LXC的安装与配置使用









