> ## Documentation Index
> Fetch the complete documentation index at: https://densumesh-broccoli-27.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# RabbitMQ

> Using RabbitMQ as your message broker

## Overview

RabbitMQ is a robust message broker with advanced routing capabilities, making it ideal for complex messaging scenarios.

## Installation

Enable RabbitMQ support in your `Cargo.toml`:

```toml theme={null}
[dependencies]
broccoli_queue = { version = "0.4", default-features = false, features = ["rabbitmq"] }
```

Or alongside Redis:

```toml theme={null}
[dependencies]
broccoli_queue = { version = "0.4", features = ["rabbitmq"] }
```

## Connection

```rust theme={null}
use broccoli_queue::queue::BroccoliQueue;

let queue = BroccoliQueue::builder("amqp://localhost:5672")
    .pool_connections(10)
    .build()
    .await?;
```

### Connection URL formats

```text theme={null}
amqp://localhost:5672                  # Basic
amqp://user:password@localhost:5672    # With credentials
amqp://user:password@localhost:5672/vhost  # With virtual host
amqps://localhost:5671                 # TLS connection
```

## Starting RabbitMQ

### Docker

```bash theme={null}
docker run -d --name rabbitmq \
  -p 5672:5672 \
  -p 15672:15672 \
  rabbitmq:management
```

Access the management UI at `http://localhost:15672` (guest/guest).

### With custom credentials

```bash theme={null}
docker run -d --name rabbitmq \
  -p 5672:5672 \
  -p 15672:15672 \
  -e RABBITMQ_DEFAULT_USER=myuser \
  -e RABBITMQ_DEFAULT_PASS=mypassword \
  rabbitmq:management
```

## Features

### Connection pooling

RabbitMQ uses `deadpool-lapin` for connection pooling:

```rust theme={null}
let queue = BroccoliQueue::builder("amqp://localhost:5672")
    .pool_connections(10)
    .build()
    .await?;
```

### Message scheduling

<Warning>
  Message scheduling with RabbitMQ requires the **delayed-exchange plugin**.
</Warning>

#### Install the plugin

```bash theme={null}
# In Docker
docker exec rabbitmq rabbitmq-plugins enable rabbitmq_delayed_message_exchange

# Or on host
rabbitmq-plugins enable rabbitmq_delayed_message_exchange
```

See the [RabbitMQ scheduling guide](https://www.rabbitmq.com/blog/2015/04/16/scheduling-messages-with-rabbitmq) for details.

#### Use scheduling

```rust theme={null}
use broccoli_queue::queue::PublishOptions;
use time::Duration;

let queue = BroccoliQueue::builder("amqp://localhost:5672")
    .enable_scheduling(true)  // Must be enabled
    .build()
    .await?;

let options = PublishOptions::builder()
    .delay(Duration::minutes(5))
    .build();

queue.publish("jobs", None, &job, Some(options)).await?;
```

## RabbitMQ concepts

Broccoli abstracts RabbitMQ concepts, but understanding them helps with debugging:

| Broccoli        | RabbitMQ                                        |
| --------------- | ----------------------------------------------- |
| Queue name      | Queue name                                      |
| `publish()`     | Publish to default exchange                     |
| `consume()`     | Basic consume with prefetch                     |
| `acknowledge()` | Basic ack                                       |
| `reject()`      | Basic nack (with requeue based on retry config) |

## Management API

With the `management` feature:

```rust theme={null}
#[cfg(feature = "management")]
{
    let status = queue.queue_status("jobs".into(), None).await?;
    println!("Queue status: {:?}", status);
}
```

## Configuration example

```rust theme={null}
use broccoli_queue::queue::{BroccoliQueue, RetryStrategy};

let queue = BroccoliQueue::builder("amqp://user:pass@localhost:5672/myapp")
    .pool_connections(15)
    .failed_message_retry_strategy(
        RetryStrategy::new()
            .with_attempts(5)
            .retry_failed(true)
    )
    .enable_scheduling(true)
    .build()
    .await?;
```

## Best practices

<AccordionGroup>
  <Accordion title="Use virtual hosts">
    Separate environments using virtual hosts:

    ```rust theme={null}
    // Development
    BroccoliQueue::builder("amqp://localhost:5672/dev")

    // Production
    BroccoliQueue::builder("amqp://localhost:5672/prod")
    ```
  </Accordion>

  <Accordion title="Enable publisher confirms">
    RabbitMQ provides publisher confirms for guaranteed delivery. Broccoli uses these internally for reliability.
  </Accordion>

  <Accordion title="Monitor via management UI">
    Use the RabbitMQ management UI (`localhost:15672`) to:

    * View queue depths
    * Monitor connection counts
    * Check message rates
    * Debug delivery issues
  </Accordion>

  <Accordion title="Configure memory limits">
    Set RabbitMQ memory limits to prevent OOM:

    ```bash theme={null}
    docker run -d --name rabbitmq \
      -e RABBITMQ_VM_MEMORY_HIGH_WATERMARK=0.6 \
      rabbitmq:management
    ```
  </Accordion>
</AccordionGroup>

## Troubleshooting

### Connection refused

```
Connection refused (os error 111)
```

Verify RabbitMQ is running:

```bash theme={null}
docker ps | grep rabbitmq
# or
rabbitmqctl status
```

### ACCESS\_REFUSED

```
ACCESS_REFUSED - Login was refused
```

Check credentials in your connection URL:

```rust theme={null}
BroccoliQueue::builder("amqp://user:password@localhost:5672")
```

### NOT\_FOUND for scheduled messages

```
NOT_FOUND - no exchange 'delayed'
```

Enable the delayed message exchange plugin:

```bash theme={null}
rabbitmq-plugins enable rabbitmq_delayed_message_exchange
```

### High memory usage

RabbitMQ can accumulate messages in memory. Monitor and set limits:

```bash theme={null}
# Check status
rabbitmqctl status

# Set watermark (fraction of available RAM)
rabbitmqctl set_vm_memory_high_watermark 0.5
```
