Class Outbox
Envelope into a stored outbox row, so the message is persisted atomically with
the business data and a separate OutboxRelay publishes it later.
Usage — the caller owns the transaction boundary (this is the whole point):
connection.setAutoCommit(false);
try {
insertOrder(connection, order); // the business write
Envelope env = EnvelopeCodec.make("urn:babel:orders:created", data, "orders", null);
outbox.write(env); // same connection, same tx
connection.commit(); // both, or neither
} catch (Exception e) {
connection.rollback();
throw e;
}
Because both writes share one transaction, a crash can never leave the business row committed without its message (the classic dual-write bug) — they commit or roll back together. The handoff to the broker becomes a local problem the relay solves.
This helper is intentionally tiny and dependency-free: it only encodes via the frozen
EnvelopeCodec (GR-1 — the envelope bytes are stored unchanged; the outbox never adds an
envelope field) and delegates persistence to the injected OutboxStore, which the caller
binds to their own DB (GR-7). It does not begin or commit anything.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe fallback queue when an envelope carries nometa.queue. -
Constructor Summary
Constructors -
Method Summary
-
Field Details
-
DEFAULT_QUEUE
The fallback queue when an envelope carries nometa.queue.- See Also:
-
-
Constructor Details
-
Outbox
- Parameters:
store- wherewrite(Envelope)persists rows; the caller binds it to their own DB
-
-
Method Details
-
write
Encode the envelope (frozen codec, bytes unchanged) and persist it via the store, inside the transaction the caller has already opened. Returns the new outbox row id.It captures the target queue from
meta.queue(falling back to"default") at write time, so the relay can publish to the right queue without ever decoding the body.- Parameters:
envelope- a canonical envelope fromEnvelopeCodec.make(java.lang.String, java.util.Map<java.lang.String, java.lang.Object>)/EnvelopeCodec.fromMessage(com.babelqueue.PolyglotMessage)- Returns:
- the outbox row id (for the caller's own correlation, if wanted)
-