springAMQP自定义direct交换机进行消息指定推送

rabbitmq中,direct交换机会提供一个BindingKey,与之对应的是发布消息时候指定的routingKey

在向交换机发布消息的时候,direct会根据附带的routingKey去找在绑定时候队列的BindingKey,如果二者匹配,则向queue推送消息

  1. 首先我们需要在rabbitmq-web端进行direct交换机的创建与绑定操作,同时我们需要指定bandingkey
  2. 我们创建两个direct_queue1&direct_queue2
  3. 在项目中进行代码实现

首先进行listener的添加(参考fanout)

复制代码
@RabbitListener(queues = "direct_queue1")
public void directQ1(String msg) throws InterruptedException {
    System.out.println("direct1接收到的msg:"+msg);
    Thread.sleep(20);
}


@RabbitListener(queues = "direct_queue2")
public void directQ2(String msg) throws InterruptedException {
    System.out.println("direct2接收到的msg:"+msg);
    Thread.sleep(20);
}

然后进行springTest的测试

复制代码
@Test
void testSendMsgToFanout(){
    String exchangeName = "cybg.direct";
    String msg = "direct everyone";
    rabbitTemplate.convertAndSend(exchangeName,"2",msg);
}

我们在这次需要指定routingkey,就是我们当时上面约定好的bindingkey

运行并查看控制台可以发现,消费者已经拿到了指定队列中的消息

相关推荐
zimoyin2 分钟前
浅浅了解下0拷贝技术
java·linux·开发语言
AI架构师易筋6 分钟前
AIOps 告警归因中的提示工程:从能用到可上生产(4 阶梯)
开发语言·人工智能·llm·aiops·rag
你的冰西瓜14 分钟前
C++中的array容器详解
开发语言·c++·stl
随丶芯30 分钟前
IDEA安装leetcode-editor插件
java·开发语言
Ccjf酷儿44 分钟前
C++语言程序设计 (郑莉)第六章 数组、指针和字符串
开发语言·c++
禹曦a1 小时前
Java实战:Spring Boot 构建电商订单管理系统RESTful API
java·开发语言·spring boot·后端·restful
superman超哥1 小时前
精确大小迭代器(ExactSizeIterator):Rust性能优化的隐藏利器
开发语言·后端·rust·编程语言·rust性能优化·精确大小迭代器
芒克芒克1 小时前
虚拟机类加载机制
java·开发语言·jvm
陌路201 小时前
C++28 STL容器--array
开发语言·c++
FPGAI1 小时前
Python之函数
开发语言·python