Overview
In AMQP 0-9-1 the queue.bind method binds a queue to an exchange so that messages flow (subject to various criteria) from the exchange (the source) to the queue (the destination). We have introduced an exchange.bind method which binds one exchange to another exchange. The binding is semantically identical to exchange-to-queue bindings: unidirectional, binding keys and exchange types operate as normal, but both endpoints (the source and destination) of the binding are exchanges. This allows far richer routing topologies to be created. Note the source and destination fields in the exchange.bind method reflect the flow of messages: from the exchange at the source, and to the exchange at the destination.
Just like with queue.bind, multiple distinct bindings can be created between the same binding-endpoints. We detect and eliminate cycles during message delivery, and ensure that transitively, over any routing topology, for every queue to which a given message is routed, each queue will receive exactly one copy of that message. Exchanges which are declared as auto-delete will still be removed when all their bindings are removed, regardless of whether those bindings are to queues or exchanges. Note that an auto-delete exchange will only be deleted when bindings for which the exchange is the source are removed: if you add exchange-to-exchange bindings for which the given exchange is the destination then that exchange will not be auto-deleted on removal of those bindings.
Java Client Example
Use the Channel#exchangeBind method. The following example binds an exchange "destination" to "source" with routing key "routingKey".
Channel ch = conn.createChannel(); ch.exchangeBind("destination", "source", "routingKey");
//待测试 发送一个消息 到 source ,routingkey 看消息是否会到达 destination routingkey 绑定的队列。
用于场景 2套程序用2个exchange ,谁都不想改,有可能交换机类型不同或者 各自的交换机下 绑定了好多队列,
这个时候发送方 发送消息到 自己的交换机所在的队列,然后同时利用exchage to exchange 功能就把消息发送到了 另外一个exchange的队列,大家都不用改东西。否则,其中一方就得修改自己代码,去连接别人的交换机。
.NET Client Example
Use the IModel#ExchangeBind method. The following example binds an exchange "destination" to "source" with routing key "routingKey".
var ch = conn.CreateModel(); ch.ExchangeBind("destination", "source", "routingKey");