当前位置: 首页 > news >正文

(2)关于RabbitMq 的 Topic Exchange 主题交换机


Topic Exchange 主题交换机

  • 一、项目里面创建TopicRabbitConfig
  • 二、然后添加多2个接口,用于推送消息到主题交换机:
  • 三、创建TopicManReceiver
  • 总结


一、项目里面创建TopicRabbitConfig

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
 
@Configuration
public class TopicRabbitConfig {
    //绑定键
    public final static String man = "topic.man";
    public final static String woman = "topic.woman";
 
    @Bean
    public Queue firstQueue() {
        return new Queue(TopicRabbitConfig.man);
    }
 
    @Bean
    public Queue secondQueue() {
        return new Queue(TopicRabbitConfig.woman);
    }
 
    @Bean
    TopicExchange exchange() {
        return new TopicExchange("topicExchange");
    }
 
 
    //将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
    //这样只要是消息携带的路由键是topic.man,才会分发到该队列
    @Bean
    Binding bindingExchangeMessage() {
        return BindingBuilder.bind(firstQueue()).to(exchange()).with(man);
    }
 
    //将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
    // 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
    @Bean
    Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(secondQueue()).to(exchange()).with("topic.#");
    }
 
}

二、然后添加多2个接口,用于推送消息到主题交换机:

    @GetMapping("/sendTopicMessage1")
    public String sendTopicMessage1() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: M A N ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> manMap = new HashMap<>();
        manMap.put("messageId", messageId);
        manMap.put("messageData", messageData);
        manMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "topic.man", manMap);
        return "ok";
    }
 
    @GetMapping("/sendTopicMessage2")
    public String sendTopicMessage2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: woman is all ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> womanMap = new HashMap<>();
        womanMap.put("messageId", messageId);
        womanMap.put("messageData", messageData);
        womanMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "topic.woman", womanMap);
        return "ok";
    }
}

三、创建TopicManReceiver

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Map;
 
 
@Component
public class TopicManReceiver {
 
    @RabbitHandler
    @RabbitListener(queues = "topic.man")
    public void process(Map testMessage) {
        System.out.println("TopicManReceiver消费者收到消息  : " + testMessage.toString());
    }
    @RabbitHandler
    @RabbitListener(queues = "topic.woman")
    public void process(Map testMessage) {
        System.out.println("TopicManReceiver消费者收到消息  : " + testMessage.toString());
    }
}

总结

:先调用/sendTopicMessage1
在这里插入图片描述
然后看消费者rabbitmq-consumer的控制台输出情况:
TopicManReceiver监听队列1,绑定键为:topic.man
TopicTotalReceiver监听队列2,绑定键为:topic.#
而当前推送的消息,携带的路由键为:topic.man

所以可以看到两个监听消费者receiver都成功消费到了消息,因为这两个recevier监听的队列的绑定键都能与这条消息携带的路由键匹配上。
在这里插入图片描述接下来调用接口/sendTopicMessage2:
在这里插入图片描述

然后看消费者rabbitmq-consumer的控制台输出情况:
TopicManReceiver监听队列1,绑定键为:topic.man
TopicTotalReceiver监听队列2,绑定键为:topic.#
而当前推送的消息,携带的路由键为:topic.woman

所以可以看到两个监听消费者只有TopicTotalReceiver成功消费到了消息。
在这里插入图片描述
到此Topic Exchange 主题交换机就算是完事了
下篇我们使用Fanout Exchang 扇型交换机

相关文章:

  • JAVA代码操作HDFS
  • web前端开发基础教程一
  • 原子尺度仿真对材料设计效率的提升,是未来材料研发的关键核心竞争力
  • CDH 10Cloudera Manager Kerberos安装配置CA配置(markdown新版三)
  • RedHat7无法安装Telnet
  • LeetCode刷题(二):前言
  • 网络套接字实现TCP机制通信
  • 一个非教条式的TDD例子
  • Spring 整合 MyBatis
  • Verilog 有符号数详解(含代码验证)
  • 今天是圣诞节, 要打印一个漂亮的圣诞树送给想象中的女朋友,请你帮助他实现梦想。
  • 同样是测试工程师,月薪8k的功能测试和月薪14k的自动化测试,差在了那里?
  • k8s 认证机制源码分析
  • Java-KoTime:接口耗时监测与邮件通知接口耗时情况
  • 【Linux】Linux系统编程(入门与系统编程)(一)(环境搭建、常见指令以及权限理解)
  • ESLint简单操作
  • Fabric架构演变之路
  • hadoop集群管理系统搭建规划说明
  • Javascript设计模式学习之Observer(观察者)模式
  • mongodb--安装和初步使用教程
  • react 代码优化(一) ——事件处理
  • react-core-image-upload 一款轻量级图片上传裁剪插件
  • RedisSerializer之JdkSerializationRedisSerializer分析
  • Unix命令
  • vue 配置sass、scss全局变量
  • 百度贴吧爬虫node+vue baidu_tieba_crawler
  • 和 || 运算
  • 回顾2016
  • 如何用vue打造一个移动端音乐播放器
  • 深入浅出webpack学习(1)--核心概念
  • 视频flv转mp4最快的几种方法(就是不用格式工厂)
  • 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿
  • 一、python与pycharm的安装
  • 一份游戏开发学习路线
  • "无招胜有招"nbsp;史上最全的互…
  • #mysql 8.0 踩坑日记
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (HAL)STM32F103C6T8——软件模拟I2C驱动0.96寸OLED屏幕
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (八)Flask之app.route装饰器函数的参数
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (附源码)apringboot计算机专业大学生就业指南 毕业设计061355
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (解决办法)ASP.NET导出Excel,打开时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致
  • (提供数据集下载)基于大语言模型LangChain与ChatGLM3-6B本地知识库调优:数据集优化、参数调整、Prompt提示词优化实战
  • (图)IntelliTrace Tools 跟踪云端程序
  • * 论文笔记 【Wide Deep Learning for Recommender Systems】
  • ***微信公众号支付+微信H5支付+微信扫码支付+小程序支付+APP微信支付解决方案总结...
  • .gitignore
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务
  • .net 中viewstate的原理和使用
  • .Net程序猿乐Android发展---(10)框架布局FrameLayout
  • .NET分布式缓存Memcached从入门到实战
  • @ComponentScan比较
  • @TableId注解详细介绍 mybaits 实体类主键注解