当前位置: 首页 > 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系统编程(入门与系统编程)(一)(环境搭建、常见指令以及权限理解)
  • @jsonView过滤属性
  • 08.Android之View事件问题
  • 10个确保微服务与容器安全的最佳实践
  • ES6--对象的扩展
  • javascript面向对象之创建对象
  • JavaSE小实践1:Java爬取斗图网站的所有表情包
  • java正则表式的使用
  • maven工程打包jar以及java jar命令的classpath使用
  • PHP 7 修改了什么呢 -- 2
  • SQLServer之索引简介
  • text-decoration与color属性
  • Vue2.x学习三:事件处理生命周期钩子
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 彻底搞懂浏览器Event-loop
  • 持续集成与持续部署宝典Part 2:创建持续集成流水线
  • 初识 webpack
  • 聊聊redis的数据结构的应用
  • 那些年我们用过的显示性能指标
  • 如何优雅地使用 Sublime Text
  • 使用 @font-face
  • 树莓派 - 使用须知
  • 微信如何实现自动跳转到用其他浏览器打开指定页面下载APP
  • 7行Python代码的人脸识别
  • # 执行时间 统计mysql_一文说尽 MySQL 优化原理
  • ## 临床数据 两两比较 加显著性boxplot加显著性
  • #、%和$符号在OGNL表达式中经常出现
  • (4)STL算法之比较
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (四)JPA - JQPL 实现增删改查
  • (原创)boost.property_tree解析xml的帮助类以及中文解析问题的解决
  • (原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)
  • ****** 二 ******、软设笔记【数据结构】-KMP算法、树、二叉树
  • ***微信公众号支付+微信H5支付+微信扫码支付+小程序支付+APP微信支付解决方案总结...
  • .chm格式文件如何阅读
  • .net core 3.0 linux,.NET Core 3.0 的新增功能
  • .NET Micro Framework初体验(二)
  • .NET Standard / dotnet-core / net472 —— .NET 究竟应该如何大小写?
  • .NET 实现 NTFS 文件系统的硬链接 mklink /J(Junction)
  • .NET 中 GetProcess 相关方法的性能
  • .net开发时的诡异问题,button的onclick事件无效