1、实现消息发送类
package cn.itcast.mq.config; import org.springframework.amqp.core.ExchangeTypes; import org.springframework.amqp.rabbit.annotation.Exchange; import org.springframework.amqp.rabbit.annotation.Queue; import org.springframework.amqp.rabbit.annotation.QueueBinding; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class Testxx { //TopicExchange订阅 @RabbitListener(bindings = @QueueBinding( value = @Queue(name = "topic.queue1"),//队列 exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),//交换机 key = "china.#" //key )) public void ListenTopicQueue1(String msg) { System.out.println("topic.queue1接受到消息" + msg); } @RabbitListener(bindings = @QueueBinding( value = @Queue(name = "topic.queue2"),//队列 exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),//交换机 key = "#.news" //key )) public void ListenTopicQueue2(String msg) { System.out.println("topic.queue2接受到消息" + msg); } }
2、重启项目
可以看到交换机和队列已生成
3、 执行测试类
@Test public void testSendTopicExchange(){ //交换机名称 String exchangeName = "itcast.topic"; //消息 String msg = "Topic嘤嘤嘤"; //发送消息 rabbitTemplate.convertAndSend(exchangeName,"china.news",msg); }
4、测试结果
可以看到key为china.#的队列和key为#.news的队列实现了china.news组合查询