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());
		}
	}

 

转载于:https://my.oschina.net/u/3698467/blog/1826933

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值