Search code examples
javagroovyfallbackjava-failsafe

Is it possible to have multiple Fallback policies using Failsafe library?


Using Failsafe library, I want to to be able to use multiple endpoints as backups whenever the primary endpoint fails. I tried the setup below, but it only executes the final fallback policy.

I couldn't find documentation about this anywhere, so I'm not sure if this is even supported. Is there a better way to achieve this objective? Other libraries I could look into as an alternative? TIA!

List<Policy> policies = []
                
policies.add(Fallback.builder({ -> sendToBackup("endpoint1") }).build())    // <--- this fallback will fail
policies.add(Fallback.builder({ -> sendToBackup("endpoint2")  }).build())   // <--- this fallback will fail
policies.add(Fallback.builder({ -> sendToBackup("endpoint3")  }).build())   // <--- this fallback will succeed
policies.add(Fallback.builder({ -> sendToBackup("endpoint4")  }).build())   // <--- this should not be executed

FailsafeExecutor executor = Failsafe.with(policies)

executor.run({ -> sendToPrimary() }  // will fail


Solution

  • Answering my own question after consulting Failsafe contributors https://github.com/failsafe-lib/failsafe/issues/374