下面显示的是英文原文。
create_with_topic
create_with_topic creates the subscription and its topic at startup where they are missing, with message ordering on.
At startup the subscription looks the topic up and creates it when it is missing. It does the same for the declared dead-letter topic, and then for the subscription itself, attached to the topic. A subscription it creates has message ordering on and carries the declared dead-letter policy. A subscription that already exists keeps its own configuration; the crate changes only its dead-letter policy, where the mount site declared one. The calls need the permissions to create topics and subscriptions, which a production service often lacks. So the step suits the emulator, tests and services that own their topology; elsewhere the topology is infrastructure and the descriptor only names it. Several subscriptions created on one topic each receive every message published to it.
Example
Two subscriptions on the topic orders: each receives every order.
- Macros
- Manual
/// The subscription `orders-workers` on the topic `orders`, with at most 100 orders unsettled.
#[subscriber(
GooglePubSub::new("orders-workers")
.create_with_topic("orders")
.max_outstanding(100)
)]
async fn place(order: &Order) -> HandlerOutcome {
tracing::info!(order.id, "order placed");
HandlerOutcome::ack()
}
/// A second subscription on the same topic: it receives every order too.
#[subscriber(GooglePubSub::new("orders-audit").create_with_topic("orders"))]
async fn audit(order: &Order) -> HandlerOutcome {
tracing::info!(order.id, "order audited");
HandlerOutcome::ack()
}
/// The subscription `orders-workers` on the topic `orders`, with at most 100 orders unsettled.
fn orders() -> GooglePubSub {
GooglePubSub::new("orders-workers")
.create_with_topic("orders")
.max_outstanding(100)
}
/// A second subscription on the same topic: it receives every order too.
fn audit() -> GooglePubSub {
GooglePubSub::new("orders-audit").create_with_topic("orders")
}