RabbitMQ 09 主题模式
主题模式
主题模式结构图:

主题模式实际上就是一种模糊匹配的模式,可以将routingKey以模糊匹配的方式去进行转发。
可以使用*或#来表示:
*:任意的一个单词。#:0个或多个单词。
定义配置类。
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* RabbitMQ配置类
*/
@Configuration
public class RabbitMqConfig { /**
* 定义交换机,可以很多个
* @return 交换机对象
*/
@Bean
public Exchange topicExchange(){
return ExchangeBuilder.topicExchange("amq.topic").build();
} /**
* 定义消息队列
* @return 消息队列对象
*/
@Bean
public Queue topicQueue(){
return new Queue("topicQueue");
} /**
* 定义绑定关系
* @return 绑定关系
*/
@Bean
public Binding binding(@Qualifier("topicExchange") Exchange exchange,
@Qualifier("topicQueue") Queue queue){
// 将定义的交换机和队列进行绑定
return BindingBuilder
// 绑定队列
.bind(queue)
// 到交换机
.to(exchange)
// 使用自定义的routingKey
.with("*.test.#")
// 不设置参数
.noargs();
}
}
定义消费者。
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component; /**
* 主题队列监听器
*/
@Component
public class TopicListener { /**
* 监听主题队列消息
*/
@RabbitListener(queuesToDeclare = {@Queue("topicQueue")})
public void receiver(String message) {
System.out.println("主题队列接收到消息:" + message);
} }
定义生产者。
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest
class RabbitMqSpringBootTests { /**
* RabbitTemplate封装了大量的RabbitMQ操作,已经由Starter提供,因此直接注入使用即可
*/
@Autowired
private RabbitTemplate rabbitTemplate; /**
* 生产者
*/
@Test
void producer() { rabbitTemplate.convertAndSend("amq.topic", "test.1", "Hello World 1");
rabbitTemplate.convertAndSend("amq.topic", "1.test", "Hello World 2");
rabbitTemplate.convertAndSend("amq.topic", "1.test.1", "Hello World 3");
} }
启动服务。

此时队列已创建。

绑定关系已设置。

启动生产者,发送消息。

可以看到,第一条消息的routingKey(
test.1)由于不匹配*.test.#而没有被队列接收到。
除了这里使用的默认主题交换机之外,还有一个叫做amq.rabbitmq.trace的交换机:

这是用于帮助记录和追踪生产者和消费者使用消息队列的交换机,它是一个内部的交换机,这里就不演示了。
- 环境
- JDK 17.0.6
- Maven 3.6.3
- SpringBoot 3.0.4
- spring-boot-starter-amqp 3.0.4
- 参考
RabbitMQ 09 主题模式的更多相关文章
- Rabbitmq(6) 主题模式
* 匹配1个 # 匹配所有 发送者: package com.aynu.bootamqp.service; import com.aynu.bootamqp.commons.utils.Amqp; i ...
- RabbitMQ消息队列(八)-通过Topic主题模式分发消息(.Net Core版)
前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过routingkey来匹配消息的模式已经有一定了解那fanout也很好 ...
- RabbitMQ (七) 订阅者模式之主题模式 ( topic )
主题模式和路由模式很像 路由模式是精确匹配 主题模式是模糊匹配 依然先通过管理后台添加一个交换机. 生产者 public class Producer { private const string E ...
- (八)RabbitMQ消息队列-通过Topic主题模式分发消息
原文:(八)RabbitMQ消息队列-通过Topic主题模式分发消息 前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过 ...
- RabbitMQ六种队列模式-主题模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主题模式 [ ...
- 队列模式&主题模式
# RabbitMQ 消息中间件 **Advanced Message Queuing Protocol (高级消息队列协议** The Advanced Message Queuing Protoc ...
- 【RabbitMQ】工作模式介绍
一.前言 之前,笔者写过< CentOS 7.2 安装 RabbitMQ> 这篇文章,今天整理一下 RabbitMQ 相关的笔记便于以后复习. 二.模式介绍 在 RabbitMQ 官网上提 ...
- RabbitMQ六种队列模式-简单队列模式
前言 RabbitMQ六种队列模式-简单队列 [本文]RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...
- RabbitMQ六种队列模式-工作队列模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列 [本文]RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...
- RabbitMQ六种队列模式-发布订阅模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅 [本文]RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...
随机推荐
- Jina AI x 矩池云Matpool |神经搜索引擎,一键构建
图片.视频.语音等非结构化数据在快速增长,随着深度学习技术的不断升级,非结构化数据的搜索也逐渐形成可能.在这样的背景下,专注于神经搜索技术的商业开源软件公司--Jina AI,提出了神经搜索 (Neu ...
- 【Azure Key Vault】Key Vault能不能生成DigiCert证书?能不能自动 Rotate 证书呢?
问题描述 因为Azure Key Vault服务上保管的证书可以轻松的与其他Azure服务集成使用,所以需要知道 Key Vault 能不能生成 DigiCert 证书?能不能自动 Rotate 证书 ...
- 在 Spring Boot 3.x 中使用 SpringDoc 2 / Swagger V3
SpringDoc V1 只支持到 Spring Boot 2.x springdoc-openapi v1.7.0 is the latest Open Source release support ...
- python执行JavaScript代码出现编码问题的解决方案
当我们安装好nodejs环境,想在python代码中去调用JavaScript代码,常常会出现编码的问题. 举个例子: python代码如下: 点击查看代码 import execjs f = ope ...
- spark-sql 与hive 常用函数
窗口函数与分析函数应用场景:(1)用于分区排序(2)动态Group By(3)Top N(4)累计计算(5)层次查询 窗口函数FIRST_VALUE:取分组内排序后,截止到当前行,第一个值LAST_V ...
- cpu过高什么原因?怎么排查?
运行大型程序或应用程序:当计算机运行大型程序或应用程序时,CPU需要处理更多的数据和指令,因此CPU占用率会相应地增加. 病毒或恶意软件:某些病毒或恶意软件会占用计算机的CPU资源来执行恶意任务,例如 ...
- 淘宝电商api接口 获取商品详情 搜索商品
iDataRiver平台 https://www.idatariver.com/zh-cn/ 提供开箱即用的taobao淘宝电商数据采集API,供用户按需调用. 接口使用详情请参考淘宝接口文档 接口列 ...
- better-scroll 1.13
简单入门示例:快速使用: <template> <div class="wrapper"> <div class="content" ...
- 摆脱鼠标系列 - vscode - Esc 返回时候 强制显示英文输入法 - ahk 脚本 - autoHotKey
为什么 摆脱鼠标系列 - vscode - Esc 返回时候 强制显示英文输入法 切换网页的时候,回来还是搜索输入法,就想到按esc,直接强制英文输入法 之前vim插件里面 用了一个 im-selec ...
- javascript web development es6 pdf js - Cheat Sheet 表格 [vuejs / webcomponents-cheatsheet-2021] 共3套
ES6 预览 VUE2 预览 webcomponents 预览 ES6 2019 pdf下载: https://files.cnblogs.com/files/pengchenggang/javasc ...