Este conteúdo não está disponível no idioma selecionado.
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}}}')sender = session.sender('engineering;{create: always, node:{type:topic, x-declare:{type:direct}}}')Copy to Clipboard Copied! Toggle word wrap Toggle overflow
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 mtry: sender = session.sender('engineering;{create: always, node:{type:topic, x-declare:{type:direct}}, assert: always}') except MessagingError, m: print mCopy to Clipboard Copied! Toggle word wrap Toggle overflow
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.