1. Failure Handling
  2. Event Listeners

Fallbacks allow you to provide an alternative result for a failed execution. They can also be used to suppress exceptions and provide a default result:

Fallback<Object> fallback = Fallback.of(defaultResult);

Throw a custom exception:

Fallback<Object> fallback = Fallback.ofException(e -> new CustomException(e.getLastFailure()));

Or compute an alternative result such as from a backup resource:

Fallback<Object> fallback = Fallback.of(this::connectToBackup);

A CompletionStage can be supplied as a fallback:

Fallback<Object> fallback = Fallback.ofStage(this::connectToBackup);

And for computations that block, a Fallback can be configured to run asynchronously:

Fallback<Object> fallback = Fallback.ofAsync(this::blockingCall);

Failure Handling

Like any FailurePolicy, Fallbacks can be configured to handle only certain results or failures:


Event Listeners

Fallbacks support event listeners that can tell you when an execution attempt failed:

fallback.onFailedAttempt(e -> log.error("Connection failed", e.getLastFailure()))

When the fallback attempt failed:

fallback.onFailure(e -> log.error("Failed to connect to backup", e.getFailure()));

Or when the execution or fallback attempt succeeded:

fallback.onSuccess(e -> log.info("Connection established"));