Binding与BindingBuilder源码解读

Binding在Spring-AMQP下core中,生产者创建消息发送至exchange,消费者从队列中消费消息,队列与交换器的绑定关系便是由Binding来表示的,类中的说明原文:

Simple container collecting information to describe a binding. Takes String destination and exchange names as arguments to facilitate wiring using code based configuration.

一、使用Binding构造函数实例化

Binding中包含一个DestinationType,指定了destination QUEUE or EXCHANGE两种类型

    public enum DestinationType {
 
        /**
         * Queue destination.
         */
        QUEUE,
 
        /**
         * Exchange destination.
         */
        EXCHANGE;
    }

 

查看其构造函数

    public Binding(String destination, DestinationType destinationType, String exchange, String routingKey,
            Map<String, Object> arguments) {
        this.destination = destination;
        this.destinationType = destinationType;
        this.exchange = exchange;
        this.routingKey = routingKey;
        this.arguments = arguments;
    }

 

实例化一个Binding即声明了一个队列与交换器的绑定关系

二、使用BindingBuilder实例化一个Binding

类的原文声明是:

Basic builder class to create bindings for a more fluent API style in code based configuration.

其主要的意思是:在配置时更加快速的创建绑定关系。实例化Binding语法:

Binding binding = BindingBuilder.bind(queue).to(exchange).with(routing_key);

将一个queue绑定到exchange并用routing_key作为绑定键

bind()返回的是一个DestinationConfigurer,根据参数类型是Queue还是Exchange选择方法

public static DestinationConfigurer bind(Queue queue) {
        return new DestinationConfigurer(queue.getName(), DestinationType.QUEUE);
    }
 
    public static DestinationConfigurer bind(Exchange exchange) {
        return new DestinationConfigurer(exchange.getName(), DestinationType.EXCHANGE);
    }

 

DestinationConfigurer类实现了不同Exchange的绑定,用to(Exchange_Type)用参数exchange类型来创建对应的DirectExchangeRoutingKeyConfigurer

public static final class DestinationConfigurer {
 
        protected final String name;
        protected final DestinationType type;
 
        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<String, Object>());
        }
 
        public HeadersExchangeMapConfigurer to(HeadersExchange exchange) {
            return new HeadersExchangeMapConfigurer(this, exchange);
        }
 
        public DirectExchangeRoutingKeyConfigurer to(DirectExchange exchange) {
            return new DirectExchangeRoutingKeyConfigurer(this, exchange);
        }
 
        public TopicExchangeRoutingKeyConfigurer to(TopicExchange exchange) {
            return new TopicExchangeRoutingKeyConfigurer(this, exchange);
        }
 
        public GenericExchangeRoutingKeyConfigurer to(Exchange exchange) {
            return new GenericExchangeRoutingKeyConfigurer(this, exchange);
        }
    }

 

最后使用with绑定routing_key,返回一个Binding实例,以DirectExchange为例

public static final class DirectExchangeRoutingKeyConfigurer extends AbstractRoutingKeyConfigurer<DirectExchange> {
 
        DirectExchangeRoutingKeyConfigurer(DestinationConfigurer destination, DirectExchange exchange) {
            super(destination, exchange.getName());
        }
 
        public Binding with(String routingKey) {
            return new Binding(destination.name, destination.type, exchange, routingKey,
                    Collections.<String, Object>emptyMap());
        }
 
        public Binding with(Enum<?> routingKeyEnum) {
            return new Binding(destination.name, destination.type, exchange, routingKeyEnum.toString(),
                    Collections.<String, Object>emptyMap());
        }
 
        public Binding withQueueName() {
            return new Binding(destination.name, destination.type, exchange, destination.name,
                    Collections.<String, Object>emptyMap());
        }
    }

 

posted @ 2018-09-02 12:41  门罗的魔术师  阅读(631)  评论(0编辑  收藏  举报