在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");
}
其中的代碼邏輯用於存放聲明的相關隊列、交換機和綁定關係。