Search

4.7.3. Create a Direct Exchange from an application

download PDF
You can create a Direct Exchange in an application as a side effect of creating a sender or a receiver. For example, the following example creates a direct exchange called engineering:
Python
sender = session.sender('engineering;{create: always, node:{type:topic, x-declare:{type:direct}}}')
In the case where an exchange named engineering already exists, the sender will not try to create a new one, but will connect to the existing one. You need to be careful, however, because if a queue with the name engineering already exists, then your sender will silently connect to that queue.
To ensure that your sender will connect to a new or existing exchange called engineering, you can use assert, as in this example:
Python
try:
  sender = session.sender('engineering;{create: always, node:{type:topic, x-declare:{type:direct}}, assert: always}')
except MessagingError, m:
  print m
When you use assert: always, node: {type: topic}; if engineering exists and is a queue, rather than an exchange, the sender constructor will raise an exception: "expected topic, got queue".
Note that while you can use assert to verify that it is an exchange and not a queue, you cannot verify what type of exchange it is.
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.