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

Most upvoted comments

It occurred to me an onHandled type of event is basically the same as RetryPolicy.onFailedAttempt. Perhaps that one can be moved up somewhere more common for use in other policies.

onHandled ?