Saga Builder
DSL builder for creating standalone saga executors.
SagaBuilder provides a fluent API for defining saga steps, compensation logic, idempotency markers, and monitors. The builder is mutable during construction but produces an immutable SagaExecutor.
Usage Example
val saga = sagaExecutor<OrderContext, OrderResult> {
step("reserve-inventory") { context ->
inventoryService.reserve(context.items) // Can be a suspend call
}
compensate { reservationId ->
inventoryService.release(reservationId) // Can be a suspend call
}
step("charge-payment") { context ->
paymentService.charge(context.amount) // Can be a suspend call
}
compensate { transactionId ->
paymentService.refund(transactionId) // Can be a suspend call
}
idempotent(true)
monitor { event ->
logger.info("Saga event: $event")
}
}
// Execute within a coroutine
val result = saga.execute(OrderContext(...))Step Configuration
Steps are configured using a fluent chain:
step(name) { ... }- Define the step's forward actioncompensate { ... }- (Optional) Define compensation for the stepidempotent(true)- (Optional) Mark the step as idempotent
Each call to step() finalizes the previous step and starts a new one.
Parameters
The type of context passed to saga steps
The type of result produced by saga steps
Properties
Functions
Marks the last step as idempotent using simple syntax.
Marks the last step as non-idempotent.
Build the saga executor.
Define compensation logic for the current step.
Adds compensation to the last defined step using simple syntax (suspend function).
Mark the current step as idempotent (safe to retry).
Add a monitor to observe saga events.
Add a monitor using a lambda.
Adds a saga monitor using "monitoring with" syntax.
Adds a saga monitor instance.
Set retry policy for current step.
Define a saga step with forward logic and optional timeout.
Define a saga step with string name (convenience overload).