Fallback

  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);

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:

fallback
  .handle(ConnectException.class)
  .handleResult(null);

Event Listeners

Fallbacks support the standard policy listeners which can notify you when a fallback fails:

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

Or succeeds:

fallback.onSuccess(e -> log.info("Connected to backup"));