A small RabbitMQ client library for Rust with a narrow API:
Connectionfor broker accessPublisherfor queue or exchange publishingConsumerfor queue consumptionRetryConfigfor retry and DLQ behaviorMessageEnvelopefor payloads that need retry metadata
[dependencies]
rust-rabbit = "1.2"
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }use rust_rabbit::{Connection, Consumer, Publisher, RetryConfig};
use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize)]
struct Order {
id: u32,
amount: f64,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let connection = Connection::new("amqp://localhost:5672").await?;
let publisher = Publisher::new(connection.clone());
publisher
.publish_to_queue("orders", &Order { id: 1, amount: 10.0 }, None)
.await?;
let consumer = Consumer::builder(connection, "orders")
.with_retry(RetryConfig::exponential_default())
.with_prefetch(5)
.build();
consumer
.consume(|order: Order| async move {
println!("processing order {}", order.id);
Ok(())
})
.await?;
Ok(())
}- Publishing to a queue declares the queue if needed.
- Publishing to an exchange declares the exchange as durable topic.
- Consumers declare their queue and optional exchange binding on startup.
- Handler errors trigger retry when
RetryConfigis set; exhausted messages go to DLQ. consume()works with raw payloads and detects MassTransit envelopes automatically.consume_envelopes()works withMessageEnvelope<T>.manual_ack()is currently not supported at runtime because handlers do not receive an ack handle.
use rust_rabbit::{DelayStrategy, RetryConfig};
use std::time::Duration;
let exponential = RetryConfig::exponential_default();
let linear = RetryConfig::linear(3, Duration::from_secs(5));
let custom = RetryConfig::custom(vec![Duration::from_secs(1), Duration::from_secs(10)]);
let delayed = RetryConfig::exponential_default()
.with_delay_strategy(DelayStrategy::DelayedExchange);See docs/retry-guide.md for retry choices and docs/queues-exchanges.md for routing patterns.
examples/basic_publisher.rsexamples/basic_consumer.rsexamples/retry_examples.rsexamples/envelope_example.rsexamples/masstransit_option_example.rsexamples/delayed_exchange_example.rsexamples/production_setup.rs
API docs: https://docs.rs/rust-rabbit