如何在Spring Boot中快速创建并使用@RabbitListener的交换机与队列实例 - 第二部分
最编程
2024-02-16 09:54:51
...
@Component
public class DirectConsumer {
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "springboot.direct.queue1"),
exchange = @Exchange(name = "springboot.direct", type = ExchangeTypes.DIRECT),
key = {"red", "blue"}
))
public void listenDirectQueue1(String msg) {
System.out.println("消费者1接收到direct.queue1的消息:【" + msg + "】");
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "springboot.direct.queue2"),
exchange = @Exchange(name = "springboot.direct", type = ExchangeTypes.DIRECT),
key = {"red", "yellow"}
))
public void listenDirectQueue2(String msg) {
System.out.println("消费者2接收到direct.queue2的消息:【" + msg + "】");
}
}
@Component
public class TopicConsumer {
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "springboot.topic.queue1"),
exchange = @Exchange(name = "springboot.topic", type = ExchangeTypes.TOPIC),
key = "china.#"
))
public void listenTopicQueue1(String msg) {
System.out.println("消费者1接收到topic.queue1的消息:【" + msg + "】");
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "springboot.topic.queue2"),
exchange = @Exchange(name = "springboot.topic", type = ExchangeTypes.TOPIC),
key = "#.news"
))
public void listenTopicQueue2(String msg) {
System.out.println("消费者2接收到topic.queue2的消息:【" + msg + "】");
}
}