1.TopicController
package com.bctc.servlet.rabbitmq.topic; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/TopicController") public class TopicController { @Autowired private TopicSender sender; @GetMapping(value = "/topic") public void topic(String a) throws Exception { sender.send(a); } @GetMapping(value = "/topic1") public void topic1(String a) throws Exception { sender.send1(a); } @GetMapping(value = "/topic2") public void topic2(String a) throws Exception { sender.send2(a); } }
2.TopicRabbitConfig
package com.bctc.servlet.rabbitmq.topic; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * topic 类型的交换器允许在RabbitMQ中使用模糊匹配来绑定自己感兴趣的信息。 */ @Configuration public class TopicRabbitConfig { final static String a = "topic.aa"; final static String b = "topic.bb"; final static String c = "topic.cc"; final static String d = "topic.dd"; //创建队列 @Bean public Queue queueMessage() { return new Queue(TopicRabbitConfig.a); } //创建队列 @Bean public Queue queueMessage2() { return new Queue(TopicRabbitConfig.b); } @Bean public Queue queueMessagec() { return new Queue(TopicRabbitConfig.c); } @Bean public Queue queueMessaged() { return new Queue(TopicRabbitConfig.d); } //创建交换器 @Bean TopicExchange exchange() { return new TopicExchange("topicExchange"); } @Bean TopicExchange exchange2() { return new TopicExchange("topicExchange2"); } @Bean //对列绑定并关联到ROUTINGKEY//topic.aa就可以监听到topic.aba的消息 Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) { BindingBuilder.TopicExchangeRoutingKeyConfigurer to = BindingBuilder.bind(queueMessage).to(exchange); return BindingBuilder.bind(queueMessage).to(exchange).with("topic.abab"); } @Bean Binding bindingExchangeMessage1(Queue queueMessage2, TopicExchange exchange) { return BindingBuilder.bind(queueMessage2).to(exchange).with("topic.abab"); } @Bean//*表示一个词,#表示零个或多个词 Binding bindingExchangeMessagec(Queue queueMessagec, TopicExchange exchange) { return BindingBuilder.bind(queueMessagec).to(exchange).with("*.c1.#");//*表示一个词,#表示零个或多个词 } @Bean//*表示一个词,#表示零个或多个词 Binding bindingExchangeMessaged(Queue queueMessaged, TopicExchange exchange2) { return BindingBuilder.bind(queueMessaged).to(exchange2).with("dd.dd.*");//*表示一个词,#表示零个或多个词 } }
3.TopicSender生产者
package com.bctc.servlet.rabbitmq.topic; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Date; @Component public class TopicSender { @Autowired private AmqpTemplate rabbitTemplate; public void send(String a) { this.rabbitTemplate.convertAndSend("topicExchange", "topic.abab", a); } public void send1(String a) { this.rabbitTemplate.convertAndSend("topicExchange", "c1.c1.15d", a); } public void send2(String a) { this.rabbitTemplate.convertAndSend("topicExchange2", "dd.dd.c112", a); } }
4.TopicReceiver消费者
package com.bctc.servlet.rabbitmq.topic; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class TopicReceiver { @RabbitListener(queues = "topic.aa") @RabbitHandler public void process(String message) { System.out.println("topic.aa Receiver1 : " + message); } /* @RabbitListener(queues = "topic.aa")//一次只有一个能接收消息 @RabbitHandler public void processs(String message) { System.out.println("topic.aa Receiveraa : " + message); }*/ @RabbitListener(queues = "topic.bb") @RabbitHandler public void process2(String message) { System.out.println("topic.bb bbbbbbb : " + message); } @RabbitListener(queues = "topic.cc") @RabbitHandler public void processc(String message) { System.out.println("topic.cc ccccccc : " + message); } @RabbitListener(queues = "topic.dd") @RabbitHandler public void processd(String message) { System.out.println("topic.dd dddddddd : " + message); } /* @RabbitListener(queues = "topic-topic.*") @RabbitHandler public void process3(String message) { System.out.println("topic-topic.* : " + message); }*/ }
http://localhost:9081/TopicController/topic?a=niuhao
http://localhost:9081/TopicController/topic1?a=niuhao
http://localhost:9081/TopicController/topic2?a=niuhao