当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
【云原生】-Docker安装部署分布式数据库 OceanBase
TCP/IP协议和互联网协议群
Oracle入门 11 - Linux 开关机及系统进程命令
Analysis of pseudo-classes and pseudo-elements
服务器硬件及RAID配置实战
Oracle入门 03 - Linux / Unix 系统基础知识
Oracle入门 09 - Linux 文件上传与下载
tidyverse笔记——tidyr包
Skywalking安装部署
浅析伪类和伪元素
Zero-Shot Learning & Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning
拉格朗日插值及其应用
Oracle入门 06 - Windows 服务器安装配置
DHCP原理与配置
es6数组/数组对象求并集、交集、差集、去重、排序
Run the NPM will pop up to ask "how are you going to open this file?"
运行 npm 会弹出询问 “你要如何打开这个文件?“
Redux state management
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
Moment.js常用方法