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.