Skip to content

Add Java API for Retry with examples #113

Description

@synox

It's hard to figure out how to use Retry with Java. An example should be provided.

so for I got this - please tell me how it can be improved.

  @Wither
    @Value
    class Meta {
        String payload;
        int attempt;
    }

    @Test
    public void retry2() throws InterruptedException {

        Flow<Tuple2<String, Meta>, Tuple2<Try<String>, Meta>, NotUsed> flow = Flow.<Tuple2<String, Meta>>create()
                .map(t -> {
                    System.out.println(t);
                    if (t._1.equals("banana")) {
                        return Tuple2.apply(Failure.apply(new IllegalArgumentException("oops")), t._2);
                    } else {
                        return Tuple2.apply(Success.apply(t._1), t._2);
                    }
                });

        Source.from(Arrays.asList("apple", "banana"))
                .map(s -> Tuple2.apply(s, new Meta(s, 1)))
                .via(Retry.apply(flow, (JFunction1<Meta, Option<Tuple2<String, Meta>>>) state -> {
                    if (state.attempt < 4) {
                        return Some.apply(Tuple2.apply(state.payload, state.withAttempt(state.attempt + 1)));
                    } else {
                        return Option.empty();
                    }
                }))
                .runForeach(t -> log.info("out: {}, state={}", t._1, t._2), mat);

        Thread.sleep(5000);

    }

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