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