SpringBoot之RabbitMQ-AmqpAdmin的使用

本文介绍了如何在SpringBoot应用中使用AmqpAdmin来管理RabbitMQ的Exchange、Queue和Binding。通过示例展示了创建与删除DirectExchange、FanoutExchange、TopicExchange、HeadersExchange及CustomExchange,创建与删除Queue,以及创建与移除Binding的操作。同时提到了AmqpAdmin的其他功能,如获取Queue信息和Properties。
摘要由CSDN通过智能技术生成

版本:

  • spring-boot:2.3.0
  • rabbitmq:3-management (Docker容器)
依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
application.yml
spring:
  rabbitmq:
    host: localhost

AmqpAdmin用于创建/删除 Exchange、Queue、Binding 和初始化RabbitMQ

交换器Exchange

创建

  • declareExchange(Exchange exchange)

    @Autowired
    AmqpAdmin amqpAdmin;
    
    @Test
    void rabbit() {
         
        DirectExchange directExchange = new DirectExchange("exchange.direct");
        amqpAdmin.declareExchange(directExchange);
    }
    

    Exchange对象的创建
    在这里插入图片描述

    • DirectExchange

      有如下三个构造器

      new DirectExchange(String name);
      
      new DirectExchange((String name, boolean durable, boolean autoDelete);
      
      new DirectExchange(String name, boolean durable, boolean autoDelete, Map<String, Object> arguments);
      
      • name:交换器名字
      • durable:是否持久化
      • autoDelete:是否自动删除
      • arguments:用以设置RabbitMQ的其它参数
      • 源码
        public DirectExchange(String name) {
                 
            super(name);
        }
        
        public DirectExchange(String name, boolean durable, boolean autoDelete) {
                 
            super(name, durable, autoDelete);
        }
        
        public DirectExchange(String name, boolean durable, boolean autoDelete, Map<String, Object> arguments) {
                 
            super(name, durable, autoDelete, arguments);
        }
        

      示例:

      DirectExchange directExchange = new DirectExchange("exchange.direct");
      
    • FanoutExchange

      同样也有三个构成器

      new FanoutExchange(String name);
      
      new FanoutExchange(String name, boolean durable, boolean autoDelete);
      
      new FanoutExchange(String name, 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DeathAndLife

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值