Event Listeners

  1. Failsafe Executor Listeners
  2. Policy Listeners
  3. Determining Success

Failsafe supports event listeners, both at the top level Failsafe API, and in the different Policy implementations.

Failsafe Executor Listeners

At the top level, Failsafe can notify you when an execution completes for all policies:

Failsafe.with(retryPolicy, circuitBreaker)
  .onComplete(e -> {
    if (e.getResult() != null)
      log.info("Connected to {}", e.getResult());
    else if (e.getFailure() != null)
      log.error("Failed to create connection", e.getFailure());
  })
  .get(this::connect);

It can notify you when an execution completes successfully for all policies:

Failsafe.with(retryPolicy, circuitBreaker)
  .onSuccess(e -> log.info("Connected to {}", e.getResult()))
  .get(this::connect);

Or when an execution fails for any policy:

Failsafe.with(retryPolicy, circuitBreaker)
  .onFailure(e -> log.error("Failed to create connection", e.getFailure()))
  .get(this::connect);

Policy Listeners

At the policy level, Failsafe can notify you when an execution succeeds or fails for a particular policy:

policy
  .onSuccess(e -> log.info("Connected to {}", e.getResult()))
  .onFailure(e -> log.error("Failed to create connection", e.getFailure()));

Additional listeners are available for retry policies, fallbacks and circuit breakers.

Determining Success

Many event listeners are based on whether an execution result is a success or failure. Each policy makes its own determination about execution success based on the policy’s failure handling configuration.

An execution is considered a success for a policy if the supplied result is a success, or if it was a failure but the policy was able to produce a successful result. An execution is considered a failure for a policy if the supplied result is a failure and the policy was unable to produce a successful result through its handling.