交换器和队列是MQ中high-level层面的构建模块,应用程序需确保在使用它们的时候就已经存在了,在使用之前需要先声明它们。以下源码是基于spring-amqp-1.7.7.RELEASE.jar的源码分析
public abstract class AbstractExchange extends AbstractDeclarable implements Exchange {
private final String name;
private final boolean durable;
private final boolean autoDelete;
private final Map arguments;
private volatile boolean delayed;
private boolean internal;
public AbstractExchange(String name) {
this(name, true, false);
}
public AbstractExchange(String name, boolean durable, boolean autoDelete) {
this(name, durable, autoDelete, (Map)null);
}
public AbstractExchange(String name, boolean durable, boolean autoDelete, Map arguments) {
this.name = name;
this.durable = durable;
this.autoDelete = autoDelete;
if (arguments != null) {
this.arguments = arguments;
} else {
this.arguments = new HashMap();
}
}
}
FanoutExchange,DirectExchange和TopicExchange等交换机都继承AbstractExchange,它们交换机的声明都最终调用得是AbstractExchange的构造方法,各个参数的含义:
public class Queue extends AbstractDeclarable {
private final String name;
private final boolean durable;
private final boolean exclusive;
private final boolean autoDelete;
private final Map arguments;
public Queue(String name) {
this(name, true, false, false);
}
public Queue(String name, boolean durable) {
this(name, durable, false, false, (Map)null);
}
public Queue(String name, boolean durable, boolean exclusive, boolean autoDelete) {
this(name, durable, exclusive, autoDelete, (Map)null);
}
public Queue(String name, boolean durable, boolean exclusive, boolean autoDelete, Map arguments) {
Assert.notNull(name, "'name' cannot be null");
this.name = name;
this.durable = durable;
this.exclusive = exclusive;
this.autoDelete = autoDelete;
this.arguments = arguments;
}
}
队列有很多重载的构造方法,但他们底层都调用了最下面的那个,参数分析:
@Bean
public Binding bindingSelectOrderExchange(Queue queryByOrderIdQueue, DirectExchange directExchange){
/**
* 绑定队列query.by到指定交换机,并指定路由key
*/
return BindingBuilder.bind(queryByOrderIdQueue).to(directExchange).with("query.by.orderId");
}
源码如下:
public final class BindingBuilder {
private BindingBuilder() {
}
//1,绑定队列
public static BindingBuilder.DestinationConfigurer bind(Queue queue) {
return new BindingBuilder.DestinationConfigurer(queue.getName(), DestinationType.QUEUE);
}
public static final class DirectExchangeRoutingKeyConfigurer extends BindingBuilder.AbstractRoutingKeyConfigurer {
private DirectExchangeRoutingKeyConfigurer(BindingBuilder.DestinationConfigurer destination, DirectExchange exchange) {
super(destination, exchange.getName(), null);
}
//2,指定路由key
public Binding with(String routingKey) {
return new Binding(this.destination.name, this.destination.type, this.exchange, routingKey, Collections.emptyMap());
}
public Binding with(Enum> routingKeyEnum) {
return new Binding(this.destination.name, this.destination.type, this.exchange, routingKeyEnum.toString(), Collections.emptyMap());
}
public Binding withQueueName() {
return new Binding(this.destination.name, this.destination.type, this.exchange, this.destination.name, Collections.emptyMap());
}
}
public static final class DestinationConfigurer {
protected final String name;
protected final DestinationType type;
private DestinationConfigurer(String name, DestinationType type) {
this.name = name;
this.type = type;
}
public Binding to(FanoutExchange exchange) {
return new Binding(this.name, this.type, exchange.getName(), "", new HashMap());
}
public BindingBuilder.HeadersExchangeMapConfigurer to(HeadersExchange exchange) {
return new BindingBuilder.HeadersExchangeMapConfigurer(this, exchange);
}
//3,绑定到DirectExchange交换机
public BindingBuilder.DirectExchangeRoutingKeyConfigurer to(DirectExchange exchange) {
return new BindingBuilder.DirectExchangeRoutingKeyConfigurer(this, exchange);
}
public BindingBuilder.TopicExchangeRoutingKeyConfigurer to(TopicExchange exchange) {
return new BindingBuilder.TopicExchangeRoutingKeyConfigurer(this, exchange);
}
public BindingBuilder.GenericExchangeRoutingKeyConfigurer to(Exchange exchange) {
return new BindingBuilder.GenericExchangeRoutingKeyConfigurer(this, exchange);
}
}
}