Show Table of Contents
4.7.3. Create a Direct Exchange from an application
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.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.