failsafe: Fallback success and failure policy listeners
Hi,
I’ve a question on using policy listeners with Fallback policy. I understand that onSuccess() is executed when the fallback is executed successfully.
However I’m observing something I didn’t quite expect. For e.g., with the below Fallback policy configured to execute on null result, I would not expect Got from fallback to be printed because the main call returns non-null and so fallback logic itself should not be executed.
Fallback<String> fallback = Fallback.of("hello")
.handleResult(null)
.onSuccess(e -> System.out.println("Got from fallback"))
.onFailure(e -> System.out.println("Failed to get from fallback"));
String result = Failsafe.with(fallback)
.get(() -> "world");
System.out.println("Result is " + result);
But I get the below output -
Got from fallback
Result is world
Why did the onSuccess() listener get executed?
And if I change the main call to return null, then the onFailure() listener is getting executed even though the fallback executes successfully and returns the fallback value.
Fallback<String> fallback = Fallback.of("hello")
.handleResult(null)
.onSuccess(e -> System.out.println("Got from fallback"))
.onFailure(e -> System.out.println("Failed to get from fallback"));
String result = Failsafe.with(fallback)
.get(() -> null);
System.out.println("Result is " + result);
Output -
Failed to get from fallback
Result is hello
Perhaps my understanding is incorrect or I’m being daft. 😅
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 17 (14 by maintainers)
Commits related to this issue
- Improve docs around policy listeners Per issue #248 — committed to failsafe-lib/failsafe by jhalterman 4 years ago
- Improve docs around policy listeners Per issue #248 — committed to failsafe-lib/failsafe by jhalterman 4 years ago
- Improve event listener docs to describe success vs failure Per issue #248 — committed to failsafe-lib/failsafe by jhalterman 4 years ago
- Improve event listener docs to describe success vs failure Per issue #248 — committed to failsafe-lib/failsafe by jhalterman 4 years ago
- Improve event listener docs to describe success vs failure Per issue #248 — committed to failsafe-lib/failsafe by jhalterman 4 years ago
- Update FallbackExecutor to apply fallback prior to postExecute Updates FallbackExecutor to apply fallback prior to postExecute. This ensures that any event handling done as part of postExecute takes ... — committed to failsafe-lib/failsafe by jhalterman 4 years ago
- Add docs for Fallback.onFailedAttempt. Per issue #248 — committed to failsafe-lib/failsafe by jhalterman 4 years ago
- Add docs for Fallback.onFailedAttempt. Per issue #248 — committed to failsafe-lib/failsafe by jhalterman 4 years ago
It occurred to me an
onHandledtype of event is basically the same asRetryPolicy.onFailedAttempt. Perhaps that one can be moved up somewhere more common for use in other policies.onHandled?