当前位置:网站首页>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
边栏推荐
猜你喜欢
引导过程和服务控制
机器学习反向传播的一些推导公式
安装和使用uView
磁盘管理与文件系统
Basic usage of Koa framework
防抖和节流
英语翻译软件-批量自动免费翻译软件支持三方接口翻译
Difficulty comparison between high concurrency and multithreading (easy to confuse)
搭建zabbix监控及邮件报警(超详细教学)
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
随机推荐
编辑时过滤当前节点及根据限制的层数过滤数据
Gradle剔除依赖演示
Markdown中的数学符号
leetcode 406. Queue Reconstruction by Height 根据身高重建队列(中等)
浅析重复线性渐变repeating-linear-gradient如何使用
QFileInfo常规方法
Oracle入门 03 - Linux / Unix 系统基础知识
Koa框架的基本使用
使用powerDesigner反向工程生成Entity
Database Principles Homework 3 — JMU
浅层了解欧拉函数
Zotero | Zotero translator插件更新 | 解决百度学术文献无法获取问题
(border-box) The difference between box model w3c and IE
OSI七层模型
shell脚本 -d 是目录文件,那么-e,-f等说明
JS函数柯里化
Oracle入门 05 - VirtualBox的虚拟机安装配置
线程中断方法
数据库概论 - MySQL的简单介绍
如何在uni-app中选择一个合适的UI组件库