此内容没有您所选择的语言版本。

4.8.6. Subscribe to a Fanout Exchange


When subscribing to a fanout exchange you have two options:
  1. Subscribe to the exchange using an ephemeral subscription. This creates and binds a temporary private queue that is destroyed when your application disconnects. This approach makes sense when you do not need to share responsibility for the messages between multiple consumers, and you do not care about messages that are sent when your application is not running or is disconnected.
  2. Subscribe to a queue that is bound to the exchange. This allows messages to be buffered in the queue when your application is disconnected, and allows several consumers to share responsibility for the messages in the queue.
Private, ephemeral subscription

To implement the private, ephemeral subscription, create a receiver using the name of the fanout exchange as the receiver's address. For example:

Python
rx = receiver("amq.fanout")
Copy to Clipboard Toggle word wrap
Shareable subscription

To implement a shareable subscription that persists across consumer application restarts, create a queue, and subscribe to that queue.

You can create and bind the queue using qpid-config:
qpid-config add queue shared-q 
qpid-config bind amq.fanout shared-q
Copy to Clipboard Toggle word wrap
Note: To make the queue persistent across broker restarts, use the --durable option.
Use the qpid-config command to view the exchange bindings after issuing these commands. On MRG Messaging 2.2 and below use the command qpid-config exchanges -b. On MRG Messaging 2.3 and above use the command qpid-config exchanges -r.
Once you have created and bound the queue, in your application create a receiver that listens to this queue:
Python
rx = receiver("shared-q")
Copy to Clipboard Toggle word wrap
You could also create and bind the queue in the application code, rather than using qpid-config:
AMQP 0-10

Python
rx = receiver("shared-q;{create: always, link: {x-bindings: [{exchange: 'amq.fanout', queue: 'shared-q'}]}}")
Copy to Clipboard Toggle word wrap

AMQP 1.0

C++
Receiver receiver = session.createReceiver("amq.fanout;{node: {capabilities:[shared]}, link: {name: 'shared-q'}}");
Copy to Clipboard Toggle word wrap

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat