当前位置:网站首页>DirectExchange交换机简单入门demo
DirectExchange交换机简单入门demo
2022-07-31 05:25:00 【Max恒】
源码在文章末尾
前置学习知识
1. FanoutExchange交换机代码教程 SpringAMQP 简单入门demod2
了解过Fanout交换机的同学就一定知道, 在给Fanout交换机发送消息的时候只需要指定交换机的名称, 和要发送的消息即可, 并且可以指定多个队列 FanoutExchange交换机代码教程
@Test
public void testSendFanoutExchange() {
// 交换机名称
String exchangeName = "声明FanoutExchange交换机";
// 消息
String message = "测试 FanoutExchange 交换机, 请各位接收 !";
rabbitTemplate.convertAndSend(exchangeName, "", message);
}下面我们要学习的Direct交换机的区别, 和Fanout很相似, 但是唯一不同的就是每一个Queue都与Exchange设置一个BindingKey, 而且在发布消息的时候也需要指定消息的RoutingKey
下面我们用代码演示一下
@RabbitListener(bindings = @QueueBinding(
//队列名称
value = @Queue("direct.queue1"),
//交换机名称
exchange = @Exchange(value = "direct", type = ExchangeTypes.DIRECT),
//key
key = {"red", "blue"}
))
public void listenDirectQueue1(String msg){
System.out.println("消费者接收到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("消费者接收到fanout.queue1的消息:【" + msg + "】");
}案例1 key的值为blue
@Test
public void testSendDirectExchange() {
// 交换机名称
String exchangeName = "direct";
// 消息
String red = "hello, blue!";
rabbitTemplate.convertAndSend(exchangeName, "blue", red);
}运行结果 :

解析:
其实原因很简单 在我们的QueueBinding中声明的key中包含了 "blue"

案例2 key值为red
@Test
public void testSendDirectExchange() {
// 交换机名称
String exchangeName = "direct";
// 消息
String red = "hello, red!";
rabbitTemplate.convertAndSend(exchangeName, "red", red);
}运行结果 :
解析 :
我们能看出, 这次由两个队列同时接收到了消息, 在队列direct.queue1和direct.queue2中, QueueBinding中声明的key同时都包含了 "red"
源码项目地址:
链接: https://pan.baidu.com/s/1vnYVEPGO8B5XLCf9Xc67cA 提取码: few9
边栏推荐
- Oracle入门 10 - Linux 设备类型与文件目录结构
- 记录一下,今天开始刷剑指offer
- 试题 历届真题 错误票据【第四届】【省赛】【B组】
- 选择排序法
- 浅层了解欧拉函数
- Database Principles Homework 2 — JMU
- 浅析v-model语法糖的实现原理与细节知识及如何让你开发的组件支持v-model
- 如何在uni-app中选择一个合适的UI组件库
- TCP/IP协议和互联网协议群
- Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
猜你喜欢

记录一下,今天开始刷剑指offer

mysql索引失效的常见9种原因详解

Oracle入门 07 - Linux 操作系统安装配置(REHL 7.x)

软链接和硬链接画图,以及代码,一级目录的解释,重定向,创建文件,删除文件,创建目录,删除目录,cp、mv命令的使用

2022年软件测试现状最新报告

引导过程和服务控制

Basic usage of Koa framework

Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model

群晖NAS配置阿里云盘同步

(border-box) The difference between box model w3c and IE
随机推荐
试题 历届真题 错误票据【第四届】【省赛】【B组】
webdriver.定位元素
Basic usage of Koa framework
VNC 启动脚本
(border-box)盒子模型w3c、IE的区别
深度解析 z-index
基本正则表达式元字符,字符,次数,锚定分组
Oracle 日期函数相关
APP测试:测试流程及常规测试内容
接口报错no message avaliable
DNS域名解析服务
什么是浮动?什么是文档流?清除浮动的几种方式及原理?什么是BFC,如何触发BFC,BFC的作用
fdisk分区,gdisk添加磁盘,parted进行磁盘分区,parted新增分区,临时挂载和永久挂载
机器学习反向传播的一些推导公式
实现二叉树的基本操作
frp内网穿透服务
TypeScript编译(tsconfig.json)
浅析v-model语法糖的实现原理与细节知识及如何让你开发的组件支持v-model
【编程题】【Scratch三级】2022.03 冬天下雪了
PXE高效批量网络装机