Messaging
krizaka-messaging — publish through the outbox, consume with retry then DLQ, process each message once.
At-least-once messaging on RabbitMQ, written once: an event is published by one transactional line, a consumer queue is declared by one line, a failing listener is retried with back-off and then dead-lettered, and a redelivered message is processed once.
<dependency>
<groupId>com.krizaka</groupId>
<artifactId>krizaka-spring-boot-starter-rabbitmq</artifactId>
</dependency>Publish an event
krizaka:
messaging:
producer: krizaka-users # stamped on every event (kz-producer)
exchanges:
events: platform.events # defaults: krizaka.events / krizaka.dlx
dead-letter: platform.dlx@Transactional
public User register(NewUser input) {
User user = users.save(input);
events.publish("evt.user.registered", 1, new UserRegistered(user.id(), user.email()));
return user;
}EventPublisher.publish writes one row to your outbox, in your transaction: the event is committed or rolled
back with the state change it announces, and the relay publishes it after the commit. The body is the bare event as
JSON; the envelope travels in AMQP headers.
The envelope
| Field | Type | Required | Description |
|---|---|---|---|
kz-type | string | yes | The routing key./^evt\.[a-z0-9-]+\.[a-z0-9-]+(\.v[0-9]+)?$/ |
kz-version | integer | yes | The contract version.≥ 1 |
kz-producer | string | yes | krizaka.messaging.producer of the publishing service. |
kz-correlation-id | string | yes | The requestId of the request that produced it (krizaka-web's MDC), or a new UUID. |
kz-occurred-at | string | yes | When it happened, ISO-8601 UTC.date-time |
The AMQP messageId is a UUID chosen when the row is written: a relay that crashes after publishing republishes
the same id, and the consumer's deduplication drops the copy. The message also carries content_type: application/json, persistent delivery and a publish timestamp.
Consume a queue
@Bean
Declarables userEvents(MessagingExchanges exchanges) {
return KrizakaQueues.consumer(exchanges, "krizaka.notifications.user-events",
"evt.user.registered", "evt.user.verified");
}
@RabbitListener(queues = "krizaka.notifications.user-events")
void onRegistered(UserRegistered event, @Header(AmqpHeaders.MESSAGE_ID) String messageId) {
if (!dedup.claim("notifications.user-events", messageId)) return; // already processed
try { welcome(event); } catch (RuntimeException e) { dedup.release("notifications.user-events", messageId); throw e; }
}KrizakaQueues.consumer declares the events (topic) and dead-letter (direct) exchanges, the quorum queue bound to
each routing key, and <queue>.dlq. The listener receives the type of its parameter — a tolerant copy of the
producer's contract, never a class named in a header.
Retry, then the dead-letter queue
The kit's listener container retries a failing listener with back-off, then republishes the message to
<queue>.dlq with its original headers plus x-exception-message, x-exception-stacktrace,
x-original-exchange and x-original-routingKey. A rejected message is never requeued.
krizaka:
messaging:
retry: # defaults
max-attempts: 5 # the first delivery included
initial: 500ms
multiplier: 2
max: 10sProcess each message once
krizaka:
messaging:
dedup:
store: jdbc # or `memory` for a service without a databaseMessageDedup.claim is one INSERT arbitrated by the primary key; release gives the claim back when the handler
fails, so the redelivery is processed instead of dropped.
CREATE TABLE processed_messages (
consumer VARCHAR(255) NOT NULL,
message_id VARCHAR(255) NOT NULL,
processed_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (consumer, message_id)
);The outbox
krizaka:
messaging:
outbox.jdbc.enabled: trueJdbcOutboxStore is the outbox written once, on PostgreSQL: append in your transaction, leasing — the relay claims
due rows in one short statement and publishes outside any transaction, two instances never take the same row, rows come
out oldest first —, back-off from one second up to krizaka.messaging.outbox.jdbc.max-backoff, and a purge after the
retention window. The persistence starter creates its table. A store of your own still wins:
lockPendingBatch must claim its rows (FOR UPDATE SKIP LOCKED), or two instances publish the same row.
Message claims are purged on a schedule too: older than krizaka.messaging.dedup.retention (7 days), every hour.
Conventions
| What | Convention | Example |
|---|---|---|
| Event routing key | evt.<aggregate>.<event> | evt.user.registered |
| Incompatible version | a new routing key .v<n> and kz-version: n | evt.user.registered.v2 |
| Consumer queue | <service>.<usage> | krizaka.notifications.user-events |
| Dead-letter queue | <queue>.dlq | krizaka.notifications.user-events.dlq |
A compatible change (a new optional field) keeps the routing key and the version. An incompatible one is published
under evt.x.y.v2 next to evt.x.y until every consumer has moved; no shared jar of event classes, ever.
Configuration
Generated from the configuration metadata — the full reference lists every artifact.
| Property | Description |
|---|---|
krizaka.messaging.consumer.enabledBooleandefault true | Whether krizaka-messaging contributes the JSON message converter and the listener container factory (retry with back-off, then <queue>.dlq). |
krizaka.messaging.dedup.purge-intervalDurationdefault 1h | Delay between two purges of the claims older than the retention. |
krizaka.messaging.dedup.retentionDurationdefault 7d | How long a claim is kept; beyond the broker's longest redelivery window, a claim protects nothing. |
krizaka.messaging.dedup.storeStoreno default | Where claims are kept: 'jdbc' (durable, shared by the instances) or 'memory' (one instance). Required to get a MessageDedup bean; never inferred from the classpath.jdbcmemory |
krizaka.messaging.dedup.tableStringdefault processed_messages | Table the claims are kept in when the store is 'jdbc'. |
krizaka.messaging.exchanges.dead-letterStringdefault krizaka.dlx | Direct exchange failed messages are dead-lettered to; must differ from the events exchange. |
krizaka.messaging.exchanges.eventsStringdefault krizaka.events | Topic exchange domain events are published to. |
krizaka.messaging.outbox.batch-sizeIntegerdefault 100 | Most rows one batch claims and publishes. |
krizaka.messaging.outbox.enabledBooleandefault true | Whether a relay is scheduled for each OutboxStore bean. |
krizaka.messaging.outbox.failure-thresholdIntegerdefault 10 | Consecutive failures (batches that threw, publishes that failed) after which the outbox health indicator reports DOWN; reset by the next success. |
krizaka.messaging.outbox.jdbc.enabledBooleandefault false | Whether the kit contributes JdbcOutboxStore, its PostgreSQL outbox, instead of a store the application writes. Off by default: a service that has its own keeps it. |
krizaka.messaging.outbox.jdbc.max-backoffDurationdefault 15m | Longest wait before a row whose publish failed is tried again; the waits double from one second up to it. |
krizaka.messaging.outbox.jdbc.tableStringdefault krizaka_outbox | Table of the outbox (schema com/krizaka/messaging/outbox/schema-postgresql.sql). |
krizaka.messaging.outbox.leaseDurationdefault 2m | How long a row claimed by a leasing store stays invisible to the other relays. Keep it longer than a batch takes to publish, or two relays publish the same row. |
krizaka.messaging.outbox.poll-intervalDurationdefault 500ms | Delay between two batches of pending rows. |
krizaka.messaging.outbox.purge-intervalDurationdefault 1h | Delay between two purges of published rows. |
krizaka.messaging.outbox.retentionDurationdefault 7d | How long published rows are kept before the purge deletes them. |
krizaka.messaging.producerStringno default | Name of this service, stamped on every event it publishes (kz-producer). Required as soon as an EventPublisher is injected; never guessed from the application name. |
krizaka.messaging.retry.initialDurationdefault 500ms | Wait before the second attempt. |
krizaka.messaging.retry.maxDurationdefault 10s | Longest wait between two attempts, not shorter than the initial wait. |
krizaka.messaging.retry.max-attemptsIntegerdefault 5 | How many times the listener runs before the message is dead-lettered, the first delivery included; at least 1. |
krizaka.messaging.retry.multiplierDoubledefault 2 | Factor between two consecutive waits; at least 1. |