4.9.6. Subscribe to a Topic Exchange
To subscribe to topic exchange, create a queue and bind it to the exchange with the desired routing key.
The following example uses
qpid-config
to create a queue named news
and bind it to the amq.topic
exchange with a wildcard that matches everything.news
, where everything is any number of period-separated terms:
qpid-config add queue news qpid-config bind amq.topic news "#.news"
Now you can listen to the
news
queue for all messages whose routing key ends with .news
:
- Python
rxnews = ssn.receiver("news")
You can also do the entire operation (create, bind, and listen) in code, by using an address like the one in the following example:
AMQP 0-10
- Python
rxnews = ssn.receiver("news;{create: always, node: {type:queue}, link:{x-bindings:[{exchange: 'amq.topic', queue: 'news', key: '#.news'}]}}")
AMQP 1.0
- C++
Receiver rxnews = ssn.createReceiver("amq.topic/#.news;{node:{capabilities:[shared]}, link:{name: 'news'}}");
You could also create an ephemeral subscription for your application, if you do not care about queuing messages when your application is disconnected or sharing responsibility for messages. This method creates and binds a temporary private queue for your application:
- Python
rxnews = ssn.receiver("amq.topic/#.news");
In topic exchange binding key wildcard matching, the
#
symbol will match any number of period-separated terms. The #
will match exactly one term.