代码示例:
1. 新建两个队列
2. 创建交换机,名字叫 hmall.topic,类型选择 topic
3. hmall.topic 交换机绑定第一步的两个队列,绑定过程中填写 RoutingKey
4. 编写消费者代码监听这两个队列
@RabbitListener(queues = "topic.queue1")
public void listenQueue05(String msg) {
System.out.println("消费者收到了topic.queue1的消息:" + msg);
}
@RabbitListener(queues = "topic.queue2")
public void listenQueue06(String msg) {
System.out.println("消费者收到了topic.queue2的消息:" + msg);
}
5. 编写发送者,给 hmall.topic 交换机发消息
@GetMapping("/mq04")
public void mq04(){
String exchangeName = "hmall.topic";
String msg = "hello, baby";
//三个参数:交换机名称、RoutingKey、要发送的消息
rabbitTemplate.convertAndSend(exchangeName, "Beijing.news", msg);
}
6. 因为 Beijing.news 符合 topic.queue2 队列的 RoutingKey,所以只有 topic.queue2 队列能收到消息
标签:topic,hmall,String,queue2,RabbitMQ,Topic,交换机,msg From: https://www.cnblogs.com/gagaya2/p/17872177.html