当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
Oracle入门 03 - Linux / Unix 系统基础知识
搭建zabbix监控及邮件报警(超详细教学)
Postgresql source code learning (34) - transaction log ⑩ - full page write mechanism
基础配置--IP地址--主机名--域名
TypeScript编译(tsconfig.json)
浅析v-model语法糖的实现原理与细节知识及如何让你开发的组件支持v-model
nohup principle
安装和使用uView
【云原生】-Docker安装部署分布式数据库 OceanBase
在级联选择器,根据不会重复的字段,来获取当前的对象
【TA-霜狼_may-《百人计划》】美术2.3 硬表面基础
gstreamer的caps event和new_segment event
高并发与多线程之间的难点对比(容易混淆)
接口报错no message avaliable
英语翻译软件-批量自动免费翻译软件支持三方接口翻译
Analysis of pseudo-classes and pseudo-elements
深度解析 z-index
2.(1)栈的链式存储、链栈的操作(图解、注释、代码)
Moment.js常用方法
第三方库-store