Usage¶
Installation¶
If you’re using uv:
uv add mersal_rabbitmq
Otherwise,
pip install mersal_rabbitmq
Configuring the transport & pub/sub¶
Use the plugin RabbitMQPlugin, to configure both the transport and pub/sub:
from mersal.core.app import Mersal
from mersal.rabbitmq.plugin import RabbitMQPluginConfig
rabbitmq_plugin_config = RabbitMQPluginConfig(
connection_uri="amqp://guest:guest@localhost:5672",
input_queue_name="my-app",
)
app = Mersal(
"my-app",
activator,
plugins=[rabbitmq_plugin_config.plugin()],
)
await app.start()
Key RabbitMQPluginConfig fields:
connection_uriA standard AMQP URI, e.g.
amqp://user:pass@host:5672/vhost.input_queue_nameThis app’s own address - the name of the queue it consumes from.
prefetch_count(default50)How many unacknowledged messages the broker will push to this transport at once.
passive_queue_check_interval(default60seconds)How often to passively verify the input queue still exists on the broker, and proactively recreate it if not.
Nonedisables the check entirely.should_declare_exchanges/should_declare_input_queue/should_bind_input_queueEach defaults to
True, letting the transport manage its own topology. Set any of them toFalseif that piece of topology is managed externally (e.g. by infrastructure-as-code) instead.direct_exchange_name/topic_exchange_name(default"mersal.direct"/"mersal.topics")Names of the two exchanges the transport declares and uses. If you’re also using
RabbitMqSubscriptionStorage, itstopic_exchange_namemust match this one.delayed_exchange_name(defaultNone)Enables deferred messages (
app.defer/app.defer_local) using the delayed message exchange plugin, which must be enabled on the broker. When set (e.g."mersal.delayed"), anx-delayed-messageexchange with that name is declared and bound to the direct exchange, so a deferred message reaches the same queue a plain send would once its delay has elapsed. Only point-to-point addresses can be deferred, and the plugin caps the delay at 232-1 ms (about 49 days). Without it, deferring raisesDeferralNotSupportedError.input_queue_declaration_options/default_queue_declaration_optionsQueueDeclarationOptionscontrolling durability/exclusivity/auto-delete/arguments - the first for this transport’s own input queue, the second for any other queue this transport declares (e.g. viacreate_queue). Both default to a durable, non-exclusive, non-auto-delete queue.
If for any reason, you don’t want to use the plugin, the transport can be
configured separately by providing an instance of
RabbitMqTransport to the transport
argument in the Mersal app constructor. Similarly, an instance of
RabbitMqSubscriptionStorage
can be given to the subscription_config argument. This is not recommended
because the plugin has some setup where the connection is shared between the
transport and the subscription storage in addition to other features.