Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Java API for Retry with examples #113

Open
synox opened this issue Jul 27, 2017 · 3 comments
Open

Add Java API for Retry with examples #113

synox opened this issue Jul 27, 2017 · 3 comments

Comments

@synox
Copy link

synox commented Jul 27, 2017

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);

    }
@johanandren
Copy link
Member

I think there should be a Java API rather. And then possibly a sample.

Adding one could be rather straight forward if you would be up for it, mostly converting between Java and Scala function types, using akka.japi.Pair instead of Tuple2, and java.util.Optional instead of Option. You should be able to find examples of the conversions in the Akka streams Java DSL code.

Are you up for doing a PR with that perhaps?

@synox
Copy link
Author

synox commented Jul 27, 2017

I agree on Java API, but currently no time to do it.

btw. there is also RestartFlow akka/akka#19950, but it does not retry the Events.

@synox synox closed this as completed Jan 9, 2021
@johanandren
Copy link
Member

Let's keep this one open so that someone can pick it up in the future.

@johanandren johanandren reopened this Jan 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants