use rust_rabbit::{Connection, MassTransitOptions, PublishOptions, Publisher};
use serde::Serialize;
#[derive(Serialize)]
struct OrderCreated {
order_id: u32,
customer_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);
let order = OrderCreated {
order_id: 12345,
customer_id: 67890,
amount: 99.99,
};
println!("Example 1: Publishing with MassTransit option (simple)");
publisher
.publish_to_exchange(
"order-exchange",
"order.created",
&order,
Some(
PublishOptions::new().with_masstransit("Contracts:OrderCreated"), ),
)
.await?;
println!("Example 2: Publishing with MassTransit option (URN format)");
publisher
.publish_to_queue(
"order-queue",
&order,
Some(
PublishOptions::new().with_masstransit("urn:message:Contracts:OrderCreated"), ),
)
.await?;
println!("Example 3: Publishing with full MassTransit options");
let mt_options = MassTransitOptions::new("Contracts:OrderCreated")
.with_correlation_id("correlation-12345")
.with_source_address("rabbitmq://myhost/order-exchange")
.with_destination_address("rabbitmq://myhost/order.created");
publisher
.publish_to_exchange(
"order-exchange",
"order.created",
&order,
Some(PublishOptions::new().with_masstransit_options(mt_options)),
)
.await?;
println!("Example 4: Publishing without MassTransit (regular format)");
publisher
.publish_to_exchange(
"order-exchange",
"order.created",
&order,
None, )
.await?;
println!("Example 5: MassTransit with expiration and priority");
publisher
.publish_to_exchange(
"order-exchange",
"order.created",
&order,
Some(
PublishOptions::new()
.with_masstransit("Contracts:OrderCreated")
.with_expiration("60000") .with_priority(5),
),
)
.await?;
println!("All messages published successfully!");
Ok(())
}