在Rabbit基础API里面声明一个Exchange、声明一个绑定、一个队列的方式如下:
使用Spring AMQP声明,就需要用Spring AMQP 的如下模式,即声明式@Bean方式。
在RabbitMQConfig中定义如下代码:
/*------------------- 声明TopicExchange:topic001 队列:queue001 交换机和队列绑定 -------------------------*/
@Bean
public TopicExchange exchange001() {
return new TopicExchange("topic001", true, false);
}
@Bean
public Queue queue001() {
//队列持久
return new Queue("queue001", true);
}
@Bean
public Binding binding001() {
return BindingBuilder.bind(queue001()).to(exchange001()).with("spring.*");
}
/*------------------- 声明TopicExchange:topic002 队列:queue002 交换机和队列绑定 -------------------------*/
@Bean
public TopicExchange exchange002() {
return new TopicExchange("topic002", true, false);
}
@Bean
public Queue queue002() {
//队列持久
return new Queue("queue002", true);
}
@Bean
public Binding binding002() {
return BindingBuilder.bind(queue002()).to(exchange002()).with("rabbit.*");
}
/*------------------- 声明队列:queue003 交换机exchange001和队列绑定 -------------------------*/
@Bean
public Queue queue003() {
//队列持久
return new Queue("queue003", true);
}
@Bean
public Binding binding003() {
return BindingBuilder.bind(queue003()).to(exchange001()).with("mq.*");
}
/*--------------------定义一个image_queue队列和一个pdf_queue------------*/
@Bean
public Queue queue_image() {
//队列持久
return new Queue("image_queue", true);
}
@Bean
public Queue queue_pdf() {
//队列持久
return new Queue("pdf_queue", true);
}
代码解析:
定义了2个交换机、5个队列,其中的交换机和队列的绑定关系入代码所示。
启动之前写的测试方法观察是否创建成功
观察管理页面,相应队列和绑定关系创建成功
SpringAMQP 声明源码分析
step1. 查看RabbitAdmin源码
step 2 . InitializingBean
在InitializingBean中初始化之前做的方法afterPropertiesSet
step3. 查看RabbitAdmin中的afterPropertiesSet方法
step4. initialize函数
创建了三个队列,用于存放交换机、队列绑定关系
public void initialize() {
if (this.applicationContext == null) {
this.logger.debug("no ApplicationContext has been set, cannot auto-declare Exchanges, Queues, and Bindings");
return;
}
this.logger.debug("Initializing declarations");
Collection<Exchange> contextExchanges = new LinkedList<Exchange>(
this.applicationContext.getBeansOfType(Exchange.class).values());
Collection<Queue> contextQueues = new LinkedList<Queue>(
this.applicationContext.getBeansOfType(Queue.class).values());
Collection<Binding> contextBindings = new LinkedList<Binding>(
this.applicationContext.getBeansOfType(Binding.class).values());
processLegacyCollections(contextExchanges, contextQueues, contextBindings);
processDeclarables(contextExchanges, contextQueues, contextBindings);
final Collection<Exchange> exchanges = filterDeclarables(contextExchanges);
final Collection<Queue> queues = filterDeclarables(contextQueues);
final Collection<Binding> bindings = filterDeclarables(contextBindings);
for (Exchange exchange : exchanges) {
if ((!exchange.isDurable() || exchange.isAutoDelete()) && this.logger.isInfoEnabled()) {
this.logger.info("Auto-declaring a non-durable or auto-delete Exchange ("
+ exchange.getName()
+ ") durable:" + exchange.isDurable() + ", auto-delete:" + exchange.isAutoDelete() + ". "
+ "It will be deleted by the broker if it shuts down, and can be redeclared by closing and "
+ "reopening the connection.");
}
}
for (Queue queue : queues) {
if ((!queue.isDurable() || queue.isAutoDelete() || queue.isExclusive()) && this.logger.isInfoEnabled()) {
this.logger.info("Auto-declaring a non-durable, auto-delete, or exclusive Queue ("
+ queue.getName()
+ ") durable:" + queue.isDurable() + ", auto-delete:" + queue.isAutoDelete() + ", exclusive:"
+ queue.isExclusive() + ". "
+ "It will be redeclared if the broker stops and is restarted while the connection factory is "
+ "alive, but all messages will be lost.");
}
}
if (exchanges.size() == 0 && queues.size() == 0 && bindings.size() == 0) {
this.logger.debug("Nothing to declare");
return;
}
this.rabbitTemplate.execute(channel -> {
declareExchanges(channel, exchanges.toArray(new Exchange[exchanges.size()]));
declareQueues(channel, queues.toArray(new Queue[queues.size()]));
declareBindings(channel, bindings.toArray(new Binding[bindings.size()]));
return null;
});
this.logger.debug("Declarations finished");
}
其中的代码逻辑用于存放声明的相关队列、交换机和绑定关系。