Перейти к основному содержимому
Эта страница ещё не переведена

Ниже показан английский оригинал.

exchange

exchange sends a policy's publishes to a named exchange instead of the default exchange.

The message name stays the routing key, and the exchange routes by it. A topic exchange matches it against binding patterns, a direct exchange matches it exactly, and a fanout exchange ignores it. Without exchange, the default exchange delivers to the queue whose name equals the routing key. RabbitMQ accepts a publish that matches no binding and drops it. Every publish policy of the crate takes the setting: ConfirmsPublish, ServerTxPublish and LapinRequest too. With declare_topology(true), the subscription that binds a queue to the exchange declares the exchange.

Example​

Every accepted order is announced on the events topic exchange under order.placed. An audit queue bound under order.* receives it.

/// Every accepted order is announced on the `events` exchange under `order.placed`.
#[subscriber(RabbitQueue::new("orders"))]
async fn accept(order: &Order, Out(events): Out<impl Publisher>) -> HandlerOutcome {
let placed = OrderPlaced { id: order.id };
if events.message(&placed).publish().await.is_err() {
return HandlerOutcome::retry();
}
HandlerOutcome::ack()
}

/// The audit service's queue takes every `order.*` event from that exchange.
#[subscriber(RabbitQueue::new("orders.audit").bind(RabbitExchange::topic("events"), "order.*"))]
async fn audit(placed: &OrderPlaced) -> HandlerOutcome {
tracing::info!(placed.id, "order placed");
HandlerOutcome::ack()
}

#[ruststream::app]
fn app() -> impl App {
let broker = LapinBroker::new("amqp://localhost:5672").declare_topology(true);
RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(broker, |b| {
b.include(accept)
.out(DefaultSlot, Publish::default().exchange("events"))
.build();
b.include(audit);
})
}