Reactive programming with Java 8 and simple-react : error handling
1 min readMar 25, 2015
simple-react provides three mechanisms to handle and recover from errors. They are :-
capture!
The capture operator allows unrecoverable errors to be handled or logged in some form
LazyFutureStream.sequentialBuilder()
.withRetrier(retrier)
.react(()->”new event1",()->”new event2")
.map(this::unreliable)
.capture(Throwable::printStackTrace)
.block();
retry!
The retry operator allows a failed task to be retried (up to configurable limits).
AsyncRetryExecutor retrier = new AsyncRetryExecutor(Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors())).
retryOn(Throwable.class).
withMaxDelay(1_000). //1 seconds
withUniformJitter(). //add between +/- 100 ms randomly
withMaxRetries(5);
LazyFutureStream.sequentialBuilder()
.withRetrier(retrier)
.react(()->”new event1",()->”new event2")
.retry(this::unreliable)
.capture(Throwable::printStackTrace)
.block();
onFail!
The onFail operator allows exceptions from a previous task to be dealt with.
List<String> results = LazyFutureStream.sequentialBuilder()
.withRetrier(retrier)
.react(()->”new event1",()->”new event2")
.retry(this::unreliable)
.onFail(e->”default”)
.peek(System.out::println)
.capture(Throwable::printStackTrace)
.block();
assertThat(results.size(),equalTo(2));
private String unreliable(Object o) {
throw new RuntimeException();}