RabbitMQ 10 头部模式
头部模式是根据头部信息来决定的,在发送的消息中是可以携带一些头部信息的(类似于HTTP),可以根据这些头部信息来决定路由到哪一个消息队列中。
- 定义配置类。
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.HeadersExchange;
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 HeadersExchange headersExchange(){
return ExchangeBuilder.headersExchange("amq.headers").build();
}
/**
* 定义消息队列
* @return 消息队列对象
*/
@Bean
public Queue headersQueue(){
return new Queue("headersQueue");
}
/**
* 定义绑定关系
* @return 绑定关系
*/
@Bean
public Binding binding(@Qualifier("headersExchange") HeadersExchange exchange,
@Qualifier("headersQueue") Queue queue){
// 将定义的交换机和队列进行绑定
return BindingBuilder
// 绑定队列
.bind(queue)
// 到交换机
.to(exchange)
// .whereAny("a", "b").exist(); // 这个是只要存在任意一个指定的头部Key就行
// .whereAny(Collections.singletonMap("a", "b")).match(); // 传入Map也行,批量指定键值对
// .whereAll("a", "b").exist(); // 这个是必须存在所有指定的的头部Key
// .whereAll(Collections.singletonMap("a", "b")).match(); // 传入Map也行,批量指定键值对
.where("test").matches("hello"); // 比如我们现在需要消息的头部信息中包含test,并且值为hello才能转发给我们的消息队列
}
}
- 定义消费者。
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 头部监听器
*/
@Component
public class HeadersListener {
/**
* 监听头部队列消息
*/
@RabbitListener(queuesToDeclare = {@Queue("headersQueue")})
public void receiver(String message) {
System.out.println("头部队列接收到消息:" + message);
}
}
- 定义生产者。
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
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() {
Message message = MessageBuilder.withBody("Hello World".getBytes()).setHeader("test","hello").build();
rabbitTemplate.convertAndSend("amq.headers", "", message);
}
}
启动生产者发送消息。

可以看到,通过头部的匹配,队列成功接收到了消息。
- 环境
- JDK 17.0.6
- Maven 3.6.3
- SpringBoot 3.0.4
- spring-boot-starter-amqp 3.0.4
- 参考
RabbitMQ 10 头部模式的更多相关文章
- RabbitMQ六种队列模式-发布订阅模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅 [本文]RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...
- RabbitMQ六种队列模式-路由模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式 [本文]RabbitMQ六种队列模式-主 ...
- 【RabbitMQ】六种模式与SpringBoot整合
添加rabbitmq的依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...
- RabbitMQ六种队列模式-简单队列模式
前言 RabbitMQ六种队列模式-简单队列 [本文]RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...
- RabbitMQ六种队列模式-工作队列模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列 [本文]RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...
- RabbitMQ六种队列模式-主题模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主题模式 [ ...
- RabbitMQ之消息模式(下)
目的: RabbitMQ之消息模式(上):https://www.cnblogs.com/huangting/p/11994539.html 消费端限流 消息的ACK与重回队列 TTL消息 死信队列 ...
- RabbitMQ六种工作模式有哪些?怎样用SpringBoot整合RabbitMQ
目录 一.RabbitMQ入门程序 二.Work queues 工作模式 三.Publish / Subscribe 发布/订阅模式 四.Routing 路由模式 五.Topics 六.Header ...
- java面试一日一题:rabbitMQ的工作模式
问题:请讲下rabbitMQ的工作模式 分析:该问题纯属概念题,需要掌握rabbtiMQ的基础知识,同时该题也是切入MQ的一个引子: 回答要点: 主要从以下几点去考虑, 1.rabbitMQ的基本概念 ...
- Rabbitmq(7) confirm模式
1. //将通道设置为comfirm模式channel.confirmSelect(); // 消息确认if(!channel.waitForConfirms()){ System.out.print ...
随机推荐
- Java 韩顺平老师的课,记的(前6章)笔记
https://www.bilibili.com/video/BV1fh411y7R8/?p=110&spm_id_from=333.880.my_history.page.click& ...
- 牛客周赛34(A~E)
A 两种情况 两个字符相同只有2 两个字符不相同4 #include <bits/stdc++.h> #define int long long #define rep(i,a,b) fo ...
- 解决windows11远程连接阿里云Centos7
本地连接CentOs7时报错 Permission denied (publickey,gssapi-keyex,gssapi-with-mic). 网上大部分说的是去修改 vim /etc/ss ...
- 来自 AI Secure 实验室的 LLM 安全排行榜简介
近来,LLM 已深入人心,大有燎原之势.但在我们将其应用于千行百业之前,理解其在不同场景下的安全性和潜在风险显得尤为重要.为此,美国白宫发布了关于安全.可靠.可信的人工智能的行政命令; 欧盟人工智能法 ...
- [VueJsDev] 其他知识 - NestJS 学习内容
[VueJsDev] 目录列表 https://www.cnblogs.com/pengchenggang/p/17037320.html NestJS 学习内容 NestJS 学习总结 Step. ...
- 动态挂载指定vue组件 Vue.extend $mount('#aaa111')
模板中要有定位 <template> <div id="aaa111"></div> </template> 指定某个函数执行 im ...
- python getOpenFileNames 获取文件实例解析
一 概念 选取文件夹 QFileDialog.getExistingDirectory() 选择文件 QFileDialog.getOpenFileName() 选择多个文件 QFileDialog. ...
- Wireshark在多媒体开发中的使用
一 概要: Wireshark(前称Ethereal)是一个网络抓包工具. 是一款非常棒的Unix和Windows上的开源 网络协议分析器.尽可能显示出最为详细的网络封包资料.Wireshark使用W ...
- Clang开发注意事项
Clang tools need their builtin headers and search for them the same way Clang does. Thus, the defaul ...
- axios封装(处理token跟get中Content-Type的请求问题)
axios封装 import axios from 'axios' //引入axios import store from '@/store/index' //引入store //此处引入router ...