当前位置:   article > 正文

RabbitMQ-topic模式_rabbittemplate topic

rabbittemplate topic

 导入Rabbit依赖包

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-amqp</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>com.fasterxml.jackson.dataformat</groupId>
  7. <artifactId>jackson-dataformat-xml</artifactId>
  8. <version>2.9.10</version>
  9. </dependency>

 配置文件进行配置Rabbit

  1. spring:
  2. rabbitmq:
  3. host: # 主机名
  4. port: # 端口
  5. virtual-host: / # 虚拟主机
  6. username: # 用户名
  7. password: # 密码
  8. listener:
  9. simple:
  10. prefetch: 1 # 每次只能获取一条消息,处理完成才能获取下一个消息
  11. # publisher-confirm-type: correlated #确认消息已发送到交换机(Exchange)
  12. # publisher-returns: true #确认消息已发送到队列(Queue)
  13. # template:
  14. # mandatory: true
  15. listener:
  16. simple:
  17. prefetch: 1 # 每次只能获取一条消息,处理完成才能获取下一个消息
  18. # acknowledge-mode: auto
  19. # retry:
  20. # enabled: true # 开启消费者失败重试
  21. # initial-interval: 1000 # 初识的失败等待时长为1
  22. # multiplier: 3 # 失败的等待时长倍数,下次等待时长 = multiplier * last-interval
  23. # max-attempts: 3 # 最大重试次数
  24. # stateless: true # true无状态;false有状态。如果业务中包含事务,这里改为false
  25. # concurrency: 2000

先配置rabbit,设置队列,交换机,key 

  1. @Configuration
  2. public class RabbitConfig
  3. //创建队列
  4.     @Bean
  5.     public Queue queueMessage() {
  6.         return new Queue("Queue1",false,false,false,null);
  7.     }
  8. //创建队列
  9.     @Bean
  10.     public Queue queueMessages() {
  11.         return new Queue("Queue2",false,false,false,null);
  12.     }
  13. //创建交换器
  14.     @Bean
  15.     TopicExchange exchange() {
  16.         return new TopicExchange("topicExchange",false,false,null);
  17.     }
  18. //对列绑定并关联到ROUTINGKEY
  19.     @Bean
  20.     Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
  21.         return BindingBuilder.bind(queueMessage).to(exchange).with("key.#");
  22.     }
  23. //对列绑定并关联到ROUTINGKEY
  24.     @Bean
  25.     Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
  26.         return BindingBuilder.bind(queueMessages).to(exchange).with("key2.#");//*表示一个词,#表示零个或多个词
  27.     }
  28. }

项目发送消息队列

  1. @Autowired
  2. private RabbitTemplate rabbitTemplate;
  1. //"topicExchange"为交换机,key1为key,msg为发送的消息
  2. rabbitTemplate.convertAndSend("topicExchange","key1",msg);

消费者进行监听

  1. @Component
  2. public class TopicListener {
  3. @RabbitListener(bindings = @QueueBinding(
  4. value = @Queue(name = "Queue1"),
  5. exchange = @Exchange(name = "topicExchange", type = ExchangeTypes.TOPIC),
  6. key = "key1"
  7. ))
  8. public void listenTopicQueue1(String msg){
  9. System.out.println("消费者接收到topic.queue1的消息:【" + msg + "】");
  10. }
  11. @RabbitListener(bindings = @QueueBinding(
  12. value = @Queue(name = "Queue2"),
  13. exchange = @Exchange(name = "topicExchange", type = ExchangeTypes.TOPIC),
  14. key = "key2"
  15. ))
  16. public void listenTopicQueue2(String msg){
  17. System.out.println("消费者接收到topic.queue2的消息:【" + msg + "】");
  18. }
  19. }


完成 localhost:15672/#/exchanges  账号密码一般都为  guest(个人设定)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/399220
推荐阅读
相关标签
  

闽ICP备14008679号