版本:
- 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,
-