1. Setup
  2. Usage
    1. Async Execution
    2. Policy Composition
    3. Cancellation
    4. Failsafe Executor

The Failsafe Retrofit integration allows Failsafe policies to be composed around Retrofit calls.


Add the latest failsafe and failsafe-retrofit dependencies to your project.


Creating a FailsafeCall that composes a policy around a Retrofit Call is straightforward:

Call call = client.newCall(request);
RetryPolicy<Response<User>> retryPolicy = RetryPolicy.ofDefaults();
FailsafeCall<User> failsafeCall = FailsafeCall.with(retryPolicy).compose(call);

// Execute with retries
Response<User> response = failsafeCall.execute();

Failure handling works just as it does with any Failsafe execution.

Async Execution

Async execution can also be performed for a FailsafeCall, which returns a CompletableFuture:

CompletableFuture<Response<User>> future = failsafeCall.executeAsync();

Policy Composition

Multiple policies can be composed around a Retrofit Call:

FailsafeCall<User> failsafeCall = FailsafeCall.with(fallback)

The same statement can also be written as:

FailsafeCall<User> failsafeCall = FailsafeCall.with(fallback, retryPolicy, circuitBreaker)

See the policy-composition docs for more details.


When a FailsafeCall is cancelled, the underlying Retrofit Call is also cancelled:


This is also true when cancellation is performed via an async execution’s future:

CompletableFuture<Response<User>> future = failsafeCall.executeAsync();

Failsafe Executor

A FailsafeExecutor configured with event listeners or an ExecutorService can be used to create a FailsafeCall:

FailsafeExecutor<Response<User>> failsafe = Failsafe.with(retryPolicy)
  .onSuccess(e ->"Found user {}", e.getResult()))
  .onFailure(e -> log.error("Failed to find user", e.getException()));
FailsafeCall<User> failsafeCall = FailsafeCall.with(failsafe).compose(call);