Class IdempotencyManager
java.lang.Object
io.github.ravocode.avoonce.core.IdempotencyManager
Core state machine that enforces exactly-once processing rules.
Three execution modes are supported:
- No hash —
execute(String, Callable): idempotency by key only. - Auto hash —
execute(String, byte[], Callable): the library hashes the raw request body using the configuredRequestHasher(SHA-256 by default). - Pre-computed hash —
execute(String, String, Callable): the caller supplies an already-computed hash string (e.g. from a signed request header).
-
Constructor Summary
ConstructorsConstructorDescriptionIdempotencyManager(IdempotencyRepository repository) Constructs anIdempotencyManagerusing the defaultSha256RequestHasher.IdempotencyManager(IdempotencyRepository repository, RequestHasher hasher) Constructs anIdempotencyManagerwith a customRequestHasher. -
Method Summary
Modifier and TypeMethodDescriptionexecute(String idempotencyKey, byte[] requestBody, Callable<IdempotencyResponse> action) Executes the given action idempotently, hashing the rawrequestBodyusing the configuredRequestHasherto detect key reuse with a different payload.execute(String idempotencyKey, String requestHash, Callable<IdempotencyResponse> action) Executes the given action idempotently using a caller-supplied pre-computed hash string.execute(String idempotencyKey, Callable<IdempotencyResponse> action) Executes the given action idempotently based on the provided key alone.
-
Constructor Details
-
IdempotencyManager
Constructs anIdempotencyManagerusing the defaultSha256RequestHasher.- Parameters:
repository- the backing store for idempotency records.
-
IdempotencyManager
Constructs anIdempotencyManagerwith a customRequestHasher.- Parameters:
repository- the backing store for idempotency records.hasher- the hashing strategy used when a raw request body is provided.
-
-
Method Details
-
execute
public IdempotencyResponse execute(String idempotencyKey, Callable<IdempotencyResponse> action) throws Exception Executes the given action idempotently based on the provided key alone. No payload validation is performed — the same key always replays the cached response.- Parameters:
idempotencyKey- The unique key identifying the idempotent request.action- The business logic to execute if this is the first attempt.- Returns:
- The cached response if previously completed, or the newly generated response.
- Throws:
IdempotencyConflictException- If another request with the same key is currently processing.Exception- If the underlying action throws an exception.
-
execute
public IdempotencyResponse execute(String idempotencyKey, byte[] requestBody, Callable<IdempotencyResponse> action) throws Exception Executes the given action idempotently, hashing the rawrequestBodyusing the configuredRequestHasherto detect key reuse with a different payload.This is the recommended overload for HTTP filters and interceptors that already buffer the request body for replay purposes.
- Parameters:
idempotencyKey- The unique key identifying the idempotent request.requestBody- The raw bytes of the HTTP request body to hash and validate.action- The business logic to execute if this is the first attempt.- Returns:
- The cached response if previously completed, or the newly generated response.
- Throws:
IdempotencyConflictException- If another request with the same key is currently processing.IdempotencyMismatchException- If the request body hash doesn't match the stored hash.Exception- If the underlying action throws an exception.
-
execute
public IdempotencyResponse execute(String idempotencyKey, String requestHash, Callable<IdempotencyResponse> action) throws Exception Executes the given action idempotently using a caller-supplied pre-computed hash string. Use this when the hash is already available (e.g. extracted from a signed request header).- Parameters:
idempotencyKey- The unique key identifying the idempotent request.requestHash- The pre-computed hash of the request payload; may benullto skip payload validation.action- The business logic to execute if this is the first attempt.- Returns:
- The cached response if previously completed, or the newly generated response.
- Throws:
IdempotencyConflictException- If another request with the same key is currently processing.IdempotencyMismatchException- If the request hash doesn't match the stored hash.Exception- If the underlying action throws an exception.
-