@Component
public class MqListener {
@RabbitListener(queues = "xzh")
public void listenXzhQueue(String mess){
System.out.println("mq消费消息:"+mess);
}
@RabbitListener(queues = "fanout.queue1")
public void listenFanoutQueue1(String mess){
System.out.println("fanout1:"+mess);
}
@RabbitListener(queues = "fanout.queue2")
public void listenFanoutQueue2(String mess){
System.out.println("fanout2:"+mess);
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue("direct.queue1"), //队列
exchange = @Exchange(value = "direct",type = ExchangeTypes.DIRECT), //交换机
key = {"red","blue"} //key
))
public void listenDirectQueue1(String mess){
System.out.println("direct.queue1"+mess);
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue("direct.queue2"),
exchange = @Exchange(value = "direct",type = ExchangeTypes.DIRECT),
key = {"red","yellow"}
))
public void listenDirectQueue2(String mess){
System.out.println("direct.queue2"+mess);
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue("topic.queue1"), //队列
exchange = @Exchange(value = "topic",type = ExchangeTypes.TOPIC), //交换机
key = "china.#" //key #表示通配符
))
public void listenTopicQueue1(String mess){
System.out.println("topic.queue1"+mess);
}
}
2.运行程序注册主题模式交换机以队列
绑定关系
3.publisher测试类发布消息 @Test
public void topicTest(){
//交换机名字
String exchangeName="topic";
//消息
String mess="中国天气现在真好";
rabbitTemplate.convertAndSend(exchangeName,"china.xx",mess);
}
结果:
4.总结: 主题模式类似路由模式,唯一区别不同就是key命名规范,以及可以使用#表示通配符,所以叫主题模式。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)