Exchange and Queue declarations.
copyright: |
|
---|---|
license: | BSD, see LICENSE for more details. |
Example creating an exchange declaration:
>>> news_exchange = Exchange("news", type="topic")
For now news_exchange is just a declaration, you can’t perform actions on it. It just describes the name and options for the exchange.
The exchange can be bound or unbound. Bound means the exchange is associated with a channel and operations can be performed on it. To bind the exchange you call the exchange with the channel as argument:
>>> bound_exchange = news_exchange(channel)
Now you can perform operations like declare() or delete():
>>> bound_exchange.declare()
>>> message = bound_exchange.Message("Cure for cancer found!")
>>> bound_exchange.publish(message, routing_key="news.science")
>>> bound_exchange.delete()
An Exchange declaration.
Parameters: |
|
---|
Name of the exchange. Default is no name (the default exchange).
AMQP defines four default exchange types (routing algorithms) that covers most of the common messaging use cases. An AMQP broker can also define additional exchange types, so see your broker manual for more information about available exchange types.
direct (default)
Direct match between the routing key in the message, and the routing criteria used when a queue is bound to this exchange.
topic
Wildcard match between the routing key and the routing pattern specified in the exchange/queue binding. The routing key is treated as zero or more words delimited by ”.” and supports special wildcard characters. “*” matches a single word and “#” matches zero or more words.
fanout
Queues are bound to this exchange with no arguments. Hence any message sent to this exchange will be forwarded to all queues bound to this exchange.
headers
Queues are bound to this exchange with a table of arguments containing headers and values (optional). A special argument named “x-match” determines the matching algorithm, where “all” implies an AND (all pairs must match) and “any” implies OR (at least one pair must match).
arguments is used to specify the arguments.
This description of AMQP exchange types was shamelessly stolen from the blog post AMQP in 10 minutes: Part 4 by Rajith Attapattu. This article is recommended reading.
The channel the exchange is bound to (if bound).
Durable exchanges remain active when a server restarts. Non-durable exchanges (transient exchanges) are purged when a server restarts. Default is True.
If set, the exchange is deleted when all queues have finished using it. Default is False.
The default delivery mode used for messages. The value is an integer, or alias string.
1 or “transient”
The message is transient. Which means it is stored in memory only, and is lost if the server dies or restarts.
- 2 or “persistent” (default)
The message is persistent. Which means the message is stored both in-memory, and on disk, and therefore preserved if the server dies or restarts.
The default value is 2 (persistent).
Additional arguments to specify when the exchange is declared.
Bind instance to channel if not already bound.
Create message instance to be sent with publish().
Parameters: |
|
---|
Declare the exchange.
Creates the exchange on the broker.
Parameters: | nowait – If set the server will not respond, and a response will not be waited for. Default is False. |
---|
Delete the exchange declaration on server.
Parameters: |
|
---|
Publish message.
Parameters: |
|
---|
Example creating a queue using our exchange in the Exchange example:
>>> science_news = Queue("science_news",
... exchange=news_exchange,
... routing_key="news.science")
For now science_news is just a declaration, you can’t perform actions on it. It just describes the name and options for the queue.
The queue can be bound or unbound. Bound means the queue is associated with a channel and operations can be performed on it. To bind the queue you call the queue instance with the channel as an argument:
>>> bound_science_news = science_news(channel)
Now you can perform operations like declare() or purge():
>>> bound_sicence_news.declare()
>>> bound_science_news.purge()
>>> bound_science_news.delete()
A Queue declaration.
Parameters: |
|
---|
Name of the queue. Default is no name (default queue destination).
The routing key (if any), also called binding key.
The interpretation of the routing key depends on the Exchange.type.
direct exchange
Matches if the routing key property of the message and the routing_key attribute are identical.
fanout exchange
Always matches, even if the binding does not have a key.
topic exchange
Matches the routing key property of the message by a primitive pattern matching scheme. The message routing key then consists of words separated by dots (”.”, like domain names), and two special characters are available; star (“*”) and hash (“#”). The star matches any word, and the hash matches zero or more words. For example “*.stock.#” matches the routing keys “usd.stock” and “eur.stock.db” but not “stock.nasdaq”.
The channel the Queue is bound to (if bound).
Durable queues remain active when a server restarts. Non-durable queues (transient queues) are purged if/when a server restarts. Note that durable queues do not necessarily hold persistent messages, although it does not make sense to send persistent messages to a transient queue.
Default is True.
Exclusive queues may only be consumed from by the current connection. Setting the ‘exclusive’ flag always implies ‘auto-delete’.
Default is False.
If set, the queue is deleted when all consumers have finished using it. Last consumer can be cancelled either explicitly or because its channel is closed. If there was no consumer ever on the queue, it won’t be deleted.
Additional arguments used when declaring the queue.
Additional arguments used when binding the queue.
Unused in Kombu, but applications can take advantage of this. For example to give alternate names to queues with automatically generated queue names.
Bind instance to channel if not already bound.
Cancel a consumer by consumer tag.
Start a queue consumer.
Consumers last as long as the channel they were created on, or until the client cancels them.
Parameters: |
|
---|
Declares the queue, the exchange and binds the queue to the exchange.
Delete the queue.
Parameters: |
|
---|
Poll the server for a new message.
Returns the message instance if a message was available, or None otherwise.
Parameters: | no_ack – If set messages received does not have to be acknowledged. |
---|
This method provides direct access to the messages in a queue using a synchronous dialogue, designed for specific types of applications where synchronous functionality is more important than performance.
Remove all ready messages from the queue.
Create the queue binding on the server.
Declare queue on the server.
Parameters: |
|
---|
Delete the binding on the server.