Class RetryingErrorHandler
java.lang.Object
io.fluxzero.sdk.tracking.RetryingErrorHandler
- All Implemented Interfaces:
ErrorHandler
- Direct Known Subclasses:
ForeverRetryingErrorHandler
An
ErrorHandler implementation that retries failed operations a configurable number of times, with optional
propagation or suppression of unrecoverable errors.
This handler is designed for scenarios where transient failures (e.g., network hiccups, temporary service downtime) are expected and recovery is possible through retrying the operation. It allows for detailed control over:
- Which errors should trigger retries (via
errorFilter) - How many retries to perform and at what delay (via
RetryConfiguration) - Whether to stop the consumer or continue on final failure
- Whether functional errors should be logged
Retry Logic:
- If the error matches
errorFilter, theretryFunctionis invoked repeatedly up tomaxRetries. - If all retries fail:
- And
stopConsumerOnFailureistrue, the original error is rethrown, halting tracking. - Otherwise, the error is logged and passed through
.
invalid reference
RetryConfiguration#getErrorMapper()
- And
- If the error does not match the filter, no retries are performed and the handler either continues or propagates, based on configuration.
Logging Behavior:
- Technical errors are logged at
ERRORlevel. FunctionalExceptions are logged atWARNlevel, iflogFunctionalErrorsistrue.- Retry success is logged via
RetryConfiguration.successLogger.
Usage Example:
@Consumer(name = "resilientHandler", errorHandler = RetryingErrorHandler.class)
public class ResilientCommandHandler {
@HandleCommand
void handle(PlaceOrder command) {
// Retries on technical failures before giving up
}
}
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a handler that retries on technical exceptions up to 5 times with a 2-second delay.RetryingErrorHandler(boolean stopConsumerOnFailure) Constructs a handler with default retry behavior and optional consumer stop behavior.RetryingErrorHandler(int maxRetries, Duration delay, Predicate<Throwable> errorFilter, boolean stopConsumerOnFailure, boolean logFunctionalErrors) Constructs a handler with detailed retry configuration options.RetryingErrorHandler(int maxRetries, Duration delay, Predicate<Throwable> errorFilter, boolean stopConsumerOnFailure, boolean logFunctionalErrors, Function<Throwable, ?> errorMapper) Constructs a fully customized retrying handler with retry behavior and error mapping logic.RetryingErrorHandler(Predicate<Throwable> errorFilter) Constructs a handler with a custom error filter that allows message tracking to continue if the error persists after retries.RetryingErrorHandler(Predicate<Throwable> errorFilter, boolean stopConsumerOnFailure) Constructs a handler with a custom error filter and optional consumer stop behavior. -
Method Summary
Modifier and TypeMethodDescriptionhandleError(Throwable error, String errorMessage, Callable<?> retryFunction) Handles the error by retrying the operation if allowed, or propagating/logging based on configuration.protected static booleanisTechnicalError(Throwable error) Determines if the error is a technical exception (notFunctionalException).protected voidLogs the error at the appropriate level based on its type.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ErrorHandler
handleError
-
Constructor Details
-
RetryingErrorHandler
public RetryingErrorHandler()Constructs a handler that retries on technical exceptions up to 5 times with a 2-second delay. Consumer is not stopped on failure. -
RetryingErrorHandler
public RetryingErrorHandler(boolean stopConsumerOnFailure) Constructs a handler with default retry behavior and optional consumer stop behavior. -
RetryingErrorHandler
-
RetryingErrorHandler
-
RetryingErrorHandler
-
RetryingErrorHandler
-
-
Method Details
-
handleError
Handles the error by retrying the operation if allowed, or propagating/logging based on configuration. Throws the original error if retries are exhausted and the tracker should stop.- Specified by:
handleErrorin interfaceErrorHandler- Parameters:
error- the encounteredThrowableerrorMessage- context about the failureretryFunction- the operation that failed- Returns:
- the final result or error after retries
-
logError
-
isTechnicalError
Determines if the error is a technical exception (notFunctionalException).
-