Skip to content

Nested retries, how to prevent duplicate retrying? #379

Description

@pandoras-toolbox

I wonder how I can do something with Failsafe.

If methods which do a retry with Failsafe are nested then how can I prevent in the outter method that a retry is performed when an exception is thrown after the last retry of a inner method?

I would like to use the same Exception type in both aaa() and in ddd(), AssertionError.class.

public static void aaa() {
    Failsafe.with(RetryPolicy.builder()
            .withMaxRetries(1)
            .handle(AssertionError.class)
            .build())
        .run(() -> {
            System.out.println("aaa");
            bbb();
        });
}

public static void bbb() {
    System.out.println("bbb");
    ccc();
    ddd();
}

public static void ccc() {
    System.out.println("ccc");
}

public static void ddd() {
    Failsafe.with(RetryPolicy.builder()
            .withMaxRetries(1)
            .handle(AssertionError.class)
            .build())
        .run(() -> {
            System.out.println("ddd");
            throw new AssertionError("ddd");
        });
}

public static void main(String[] args) {
    MyClass.aaa();
    MyClass.bbb();
}

The console output should be:

  • aaa
  • bbb
  • ccc
  • ddd
  • ddd

But it is:

  • aaa
  • bbb
  • ccc
  • ddd
  • ddd
  • aaa
  • bbb
  • ccc
  • ddd
  • ddd

I guess it could be done if there would be something like a marker or so, to flag the exception thrown by a inner retry method so that it if the flag is present it does not do a retry in the outter method, even if the exception types match.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions