SpringBoot的RabbitMQ消息队列: 六、第五模式"Topics"

Topics模式,官方的解释是Receiving messages based on a pattern (topics),它的结构是

消费者各自监控自己的队列;交换机通过一种模式策略确定生产者的消息放入那个队列。

配置文件。增加类TopicRabbitConfig,编制代码为

[java]  view plain  copy
  1. package com.example;  
  2.   
  3. import org.springframework.amqp.core.Binding;  
  4. import org.springframework.amqp.core.BindingBuilder;  
  5. import org.springframework.amqp.core.Queue;  
  6. import org.springframework.amqp.core.TopicExchange;  
  7. import org.springframework.context.annotation.Bean;  
  8. import org.springframework.context.annotation.Configuration;  
  9.   
  10. @Configuration  
  11. public class TopicRabbitConfig {  
  12.   
  13.     final static String message = "topic.message";  
  14.     final static String messages = "topic.messages";  
  15.   
  16.     @Bean  
  17.     public Queue queueMessage() {  
  18.         return new Queue(TopicRabbitConfig.message);  
  19.     }  
  20.   
  21.     @Bean  
  22.     public Queue queueMessages() {  
  23.         return new Queue(TopicRabbitConfig.messages);  
  24.     }  
  25.   
  26.     @Bean  
  27.     TopicExchange exchange() {  
  28.         return new TopicExchange("exchange");  
  29.     }  
  30.   
  31.     @Bean  
  32.     Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {  
  33.         return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");  
  34.     }  
  35.   
  36.     @Bean  
  37.     Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {  
  38.         return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");  
  39.     }  
  40. }  
7、消息发送。增加类TopicSender,编制代码为

[java]  view plain  copy
  1. package com.example;  
  2.   
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.amqp.core.AmqpTemplate;  
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. @Component  
  10. public class TopicSender {  
  11.   
  12.   
  13.     protected static Logger logger=LoggerFactory.getLogger(TopicSender.class);   
  14.       
  15.     @Autowired  
  16.     private AmqpTemplate rabbitTemplate;  
  17.     public void send1() {  
  18.         String context = "hi, i am message 1";  
  19.         logger.debug("Sender : " + context);  
  20.         this.rabbitTemplate.convertAndSend("exchange""topic.message", context);  
  21.     }  
  22.   
  23.     public void send2() {  
  24.         String context = "hi, i am messages 2";  
  25.         logger.debug("Sender : " + context);  
  26.         this.rabbitTemplate.convertAndSend("exchange""topic.messages", context);  
  27.     }  
  28. }  
8、消息接收1.增加类TopicReceiver,编制代码为

[java]  view plain  copy
  1. package com.example;  
  2.   
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.amqp.rabbit.annotation.RabbitHandler;  
  6. import org.springframework.amqp.rabbit.annotation.RabbitListener;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. @Component  
  10. @RabbitListener(queues = "topic.message")  
  11. public class TopicReceiver {  
  12.   
  13.     protected static Logger logger=LoggerFactory.getLogger(TopicReceiver.class);   
  14.       
  15.     @RabbitHandler  
  16.     public void process(String message) {  
  17.         logger.debug("Topic Receiver1  : " + message);  
  18.     }  
  19.   
  20. }  
9、消息接收2.增加类TopicReceiver2,编制代码为
[java]  view plain  copy
  1. package com.example;  
  2.   
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.amqp.rabbit.annotation.RabbitHandler;  
  6. import org.springframework.amqp.rabbit.annotation.RabbitListener;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. @Component  
  10. @RabbitListener(queues = "topic.messages")  
  11. public class TopicReceiver2 {  
  12.       
  13.     protected static Logger logger=LoggerFactory.getLogger(TopicReceiver2.class);   
  14.       
  15.   
  16.     @RabbitHandler  
  17.     public void process(String message) {  
  18.         logger.debug("Topic Receiver2  : " + message);  
  19.     }  
  20.   
  21. }  
10、RestController。增加类TopicController,编制代码为

[java]  view plain  copy
  1. package com.example;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RestController;  
  6.   
  7. @RestController  
  8. public class TopicController {  
  9.       
  10.     @Autowired  
  11.     private TopicSender topicSender;  
  12.       
  13.     @RequestMapping("/send1")  
  14.     public String send1() {  
  15.         topicSender.send1();  
  16.         return "send1 ok";  
  17.     }  
  18.       
  19.     @RequestMapping("/send2")  
  20.     public String send2() {  
  21.         topicSender.send2();  
  22.         return "send2 ok";  
  23.     }  
  24. }  
11、运行工程。在工程所在文件夹打开cmd,输入mvn spring-boot:run

12、在浏览器中分别输入http://localhost:9080/send1 ,http://localhost:9080/send2,查看控制台输出
http://localhost:9080/send1,控制台的输出,两个接收器都收到消息

http://localhost:9080/send2,控制台的输出,第二个接收器收到消息

13、小结

A、在配置文件中,定义了一个TopicExchange,然后对两个队列,分别配置了绑定规则。(变更绑定规则测试时,先停止命令行的spring-boot,再删除rabbitmq management中的队列)。

B、发送器,发送send1会匹配到topic.#和topic.message 两个Receiver都可以收到消息,发送send2只有topic.#可以匹配所有只有Receiver2监听到消息。

     发送器在发送消息时,使用的方法是需要传入一个特定的交换机的。

C、接收器,依然各自监控自己的队列;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值