RabbitMQ API Reference¶
The RabbitMQ client provides a high-level interface for working with RabbitMQ.
Core Components¶
RabbitClient¶
The main client class for interacting with RabbitMQ.
from zephcast.aio.rabbit.client import RabbitClient
from zephcast.aio.rabbit.config import RabbitConfig
client = RabbitClient(
stream_name="my-routing-key",
config=RabbitConfig(
queue_name="my-queue",
rabbitmq_url="amqp://guest:guest@localhost:5672/",
exchange_name="my-exchange" # Optional
)
)
Methods¶
connect()- Establish connection to RabbitMQclose()- Close the connectionsend(message)- Send a message with the configured routing key__aiter__()- Async iterator for receiving messages (directly iterate over the client)ack(message)- Acknowledge a messagenack(message, requeue=True)- Negative acknowledge a message
Configuration Options¶
stream_name- Routing keyconfig- RabbitConfig object with the following options:queue_name- Queue namerabbitmq_url- RabbitMQ connection URLexchange_name- Exchange name (optional)exchange_type- Exchange type (optional)exchange_durable- Exchange durability (optional)queue_durable- Queue durability (optional)auto_ack- Automatic acknowledgment (optional)prefetch_count- Consumer prefetch count (optional)
Usage Example¶
```python import asyncio from zephcast.aio.rabbit.client import RabbitClient from zephcast.aio.rabbit.config import RabbitConfig
async def rabbitmq_example(): client = RabbitClient( stream_name="my-routing-key", config=RabbitConfig( queue_name="my-queue", rabbitmq_url="amqp://guest:guest@localhost:5672/" ) )
await client.connect()
try:
await client.send("Hello RabbitMQ!")
async for message in client:
print(f"Received: {message}")
await client.ack(message)
break
finally:
await client.close()
asyncio.run(rabbitmq_example())