当前位置:   article > 正文

rabbitmq中创建交换机以及消息队列的方式和代码_rabbitmq会根据配置类创建交换机吗

rabbitmq会根据配置类创建交换机吗

一般mq这种消息中间件都是在服务端创建而非在管理端创建

1.先导依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-amqp</artifactId>
  4. </dependency>

2.先建立一个constants的包里面存放的都是一个静态变量

  1. package cn.itcast.hotel.constants;
  2. public class MqConstants {
  3. //交换机
  4. public final static String HOTEL_EXCHANGE="hotel.topic";
  5. //增改消息队列
  6. public final static String HOTEL_INSERT_QUEUE="hotel.insert.queue";
  7. //删除消息队列
  8. public final static String HOTEL_DELETE_QUEUE="hotel.delete.queue";
  9. //增改Routingkey
  10. public final static String HOTEL_INSERT_KEY="hotel.insert";
  11. //删除Routingkey
  12. public final static String HOTEL_DELETE_KEY="hotel.delete";
  13. }

3.然后利用config中bean的方式来创建具体的bean

  1. package cn.itcast.hotel.config;
  2. import cn.itcast.hotel.constants.MqConstants;
  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. @Configuration
  10. public class MqConfig {
  11. @Bean
  12. public TopicExchange topicExchange(){
  13. return new TopicExchange(MqConstants.HOTEL_EXCHANGE,true,false);
  14. }
  15. @Bean
  16. public Queue insertQueue(){
  17. return new Queue(MqConstants.HOTEL_INSERT_QUEUE,true);
  18. }
  19. @Bean
  20. public Queue deleteQueue(){
  21. return new Queue(MqConstants.HOTEL_DELETE_QUEUE,true);
  22. }
  23. @Bean
  24. public Binding insertQueueBinding(){
  25. return BindingBuilder.bind(insertQueue()).to(topicExchange()).with(MqConstants.HOTEL_INSERT_KEY);
  26. }
  27. @Bean
  28. public Binding deleteQueueBinding(){
  29. return BindingBuilder.bind(deleteQueue()).to(topicExchange()).with(MqConstants.HOTEL_DELETE_KEY);
  30. }
  31. }

4.配置yml文件

  1. spring:
  2. rabbitmq:
  3. host: 192.168.125.131
  4. port: 5672
  5. username: itcast
  6. password: 123321
  7. virtual-host: /

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

闽ICP备14008679号