自動(dòng)聲明
直接把要自動(dòng)聲明的組件Bean納入到spring容器中管理即可。
自動(dòng)聲明發(fā)生在rabbitmq第一次連接創(chuàng)建的時(shí)候。
如果系統(tǒng)從啟動(dòng)到停止沒(méi)有創(chuàng)建任何連接,則不會(huì)自動(dòng)創(chuàng)建
自動(dòng)聲明支持單個(gè)和批量
自動(dòng)聲明(創(chuàng)建)的條件
1:要有連接產(chǎn)生
2:spring容器中要有RabbitAdmin的Bean议薪,且autoStartup必須為true(默認(rèn))
3:如果ConnectionFactory使用的是CachingConnectionFactory,則CacheMode必須要是CacheMode. CHANNEL(默認(rèn))
4:所要聲明的組件的shouldDeclare必須要是true(默認(rèn))
5:Queue隊(duì)列的名字不能以amq.開頭
自動(dòng)聲明的源碼在RabbitAdmin的afterPropertiesSet方法
自動(dòng)聲明的相關(guān)代碼
package com.edu.mq.spring.rabbitAdmin;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* 類說(shuō)明:自動(dòng)聲明
*
* @author zhangkewei
* @date 2018/11/22下午9:55
*/
@ComponentScan
@Configuration
public class AutoDeclare {
/**
* 自動(dòng)聲明queue
* @return
*/
@Bean
public Queue debugQueue() {
return new Queue("testAuto.debug.queue", true);
}
@Bean
public Queue infoQueue() {
return new Queue("testAuto.info.queue", true);
}
@Bean
public Queue errorQueue() {
return new Queue("testAuto.error.queue", true);
}
@Bean
public Queue amqQueue() {
return new Queue("testAuto.amq.log", true);
}
@Bean
public Queue pay() {
Queue q = new Queue("pay", true);
q.setShouldDeclare(true);
return q;
}
/**
* 自動(dòng)聲明綁定
* @return
*/
@Bean
public Binding b1() {
return new Binding("debug.queue", Binding.DestinationType.QUEUE, "log.direct.exchange", "debug", new HashMap<>());
}
@Bean
public Binding b2() {
return new Binding("info.queue", Binding.DestinationType.QUEUE, "log.direct.exchange", "info", new HashMap<>());
}
@Bean
public Binding b3() {
return new Binding("error.queue", Binding.DestinationType.QUEUE, "log.direct.exchange", "error", new HashMap<>());
}
/**
* 一次性批量生成多個(gè)隊(duì)列
* @return
*/
@Bean
public List<Queue> logQueues(){
List<Queue> list = new ArrayList<>();
list.add(new Queue("testAutoBatch.log.debug", true));
list.add(new Queue("testAutoBatch.log.info", true));
list.add(new Queue("testAutoBatch.log.error", true));
return list;
}
/**
* 一次性批量生成多個(gè)exchange
* @return
*/
@Bean
public List<Exchange> logExchanges(){
List<Exchange> list = new ArrayList<>();
list.add(new TopicExchange("testAutoBatch.debug.topic.exchange", true, false));
list.add(new TopicExchange("testAutoBatch.info.topic.exchange", true, false));
list.add(new TopicExchange("testAutoBatch.error.topic.exchange", true, false));
return list;
}
/**
* 一次性批量生成多個(gè)exchange
* @return
*/
@Bean
public List<Binding> listBindings() {
List<Binding> list = new ArrayList<>();
list.add(BindingBuilder.bind(new Queue("testAutoBatch.log.debug")).to(new TopicExchange("debug.topic.exchange")).with("debug.*"));
list.add(BindingBuilder.bind(new Queue("testAutoBatch.log.info")).to(new TopicExchange("debug.topic.exchange")).with("info.*"));
list.add(BindingBuilder.bind(new Queue("testAutoBatch.log.error")).to(new TopicExchange("debug.topic.exchange")).with("error.*"));
return list;
}
/**
* 單獨(dú)申請(qǐng)一個(gè)exchange
* @return
*/
@Bean
public Exchange directExchange(){
return new DirectExchange("test.directExchange",true,false,null);
}
@Bean
public Exchange topicExchange(){
return new TopicExchange("test.topicExchange",true,false,null);
}
@Bean
public Exchange headersExchange(){
return new HeadersExchange("test.headersExchange",true,false,null);
}
@Bean
public Exchange fanoutExchange(){
return new FanoutExchange("test.fanoutExchange",true,false,null);
}
}
自動(dòng)聲明結(jié)果
自動(dòng)聲明的exchange
自動(dòng)聲明的隊(duì)列