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")
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
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")
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'}]}}")

AMQP 1.0

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

Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.