Event Listeners

  1. Failsafe Executor Listeners
  2. Policy Listeners

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 and circuit breakers.