Package com.babelqueue.outbox
Class InMemoryOutboxStore
java.lang.Object
com.babelqueue.outbox.InMemoryOutboxStore
- All Implemented Interfaces:
OutboxStore
Process-local reference
OutboxStore backed by a map — for tests and single-process demos.
It has no real transaction: save(byte[], String) just appends, so it cannot deliver
the atomic-with-the-business-write guarantee a production store gives. Use a database-backed
adapter (a JDBC one, binding save to the caller's open transaction) in production.
It still faithfully models the relay contract: rows are pending until
markPublished(List), fetchUnpublished(int) returns them oldest-first (a
LinkedHashMap preserves insertion order), and markFailed(String, String) bumps the
attempt count and stores the last error while leaving the row pending for retry. The body is held
(and returned) as a verbatim copy of the bytes it was stored with — never re-encoded.
Not thread-safe; intended for single-threaded tests/demos. It does not implement the
claim/lock (FOR UPDATE SKIP LOCKED) that a concurrent production relay needs — that is the
adapter's job (ADR-0029 §Scope).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintattemptsOf(String id) Test/inspection helper: the recorded attempt count for one row (0 if unknown).fetchUnpublished(int limit) Reserve up tolimitrows that are pending publish, oldest first, so a relay can forward them.lastErrorOf(String id) Test/inspection helper: the last recorded error for one row ("" if none).voidmarkFailed(String id, String error) Record a failed publish attempt for one row: increment its attempt counter and store the last error, leaving it pending so a later relay pass retries it (at-least-once).voidmarkPublished(List<String> ids) Mark the given outbox rows as successfully published (so they are never relayed again).intTest/inspection helper: the number of rows still pending publish.Persist one encoded envelope into the outbox, within the transaction the caller has already opened around its business write.
-
Constructor Details
-
InMemoryOutboxStore
public InMemoryOutboxStore()
-
-
Method Details
-
save
Description copied from interface:OutboxStorePersist one encoded envelope into the outbox, within the transaction the caller has already opened around its business write. Returns the new row's outbox id (the store's own primary key — NOTmeta.id), which the caller may keep for correlation. The body is stored verbatim; an implementation must not re-encode or mutate it.- Specified by:
savein interfaceOutboxStore- Parameters:
encoded- theEnvelopeCodec.encode(com.babelqueue.Envelope)output as UTF-8 bytesqueue- the logical target queue, captured for the relay- Returns:
- the outbox row id
-
fetchUnpublished
Description copied from interface:OutboxStoreReserve up tolimitrows that are pending publish, oldest first, so a relay can forward them. Implementations SHOULD lock/claim the rows they return (e.g.SELECT … FOR UPDATE SKIP LOCKED, or apicked_atclaim) so two concurrent relays do not both publish the same row; at-least-once still tolerates a rare double send. That claim/lock is the adapter's responsibility — the in-memory reference does not implement it.- Specified by:
fetchUnpublishedin interfaceOutboxStore- Parameters:
limit- maximum rows to return (a positive batch size)- Returns:
- pending rows, oldest first; empty when the outbox is drained
-
markPublished
Description copied from interface:OutboxStoreMark the given outbox rows as successfully published (so they are never relayed again). Called by the relay only after the transport accepted the message.- Specified by:
markPublishedin interfaceOutboxStore- Parameters:
ids- outbox row ids previously returned byOutboxStore.fetchUnpublished(int)
-
markFailed
Description copied from interface:OutboxStoreRecord a failed publish attempt for one row: increment its attempt counter and store the last error, leaving it pending so a later relay pass retries it (at-least-once). A store MAY move a row that exceeds a max-attempts threshold to a terminal/parked state, but that policy is the adapter's, not the core's.- Specified by:
markFailedin interfaceOutboxStore- Parameters:
id- the outbox row iderror- a short, human-readable failure reason (never secrets)
-
pendingCount
public int pendingCount()Test/inspection helper: the number of rows still pending publish. -
attemptsOf
Test/inspection helper: the recorded attempt count for one row (0 if unknown). -
lastErrorOf
Test/inspection helper: the last recorded error for one row ("" if none).
-