Skip to content

Instantly share code, notes, and snippets.

@nkonev
Last active October 13, 2019 01:38
Show Gist options
  • Save nkonev/389835ce08f8c382f7ffb1898b4b1eab to your computer and use it in GitHub Desktop.
Save nkonev/389835ce08f8c382f7ffb1898b4b1eab to your computer and use it in GitHub Desktop.
Error handling in Reactor-Core
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.Arrays;
import java.util.List;
public class RxExperiment {
private static final Logger LOGGER = LoggerFactory.getLogger(RxExperiment.class);
// https://github.com/reactor/reactor-core/issues/49#issuecomment-205367739
public static void main(String[] args) {
List<Integer> tickets = Arrays.asList(2, 1, 6, 8, 9, 11, 13, 15);
Flux.fromIterable(tickets)
.publish() // Turn source into hot Publisher
.autoConnect() // Instructs the hot Publisher to start when at least one `Subscriber` subscribes
.flatMap(integer -> {
if (integer % 3 == 0) {
throw new RuntimeException("divided by 3");
}
return Mono.just("" + integer);
}) //Business logic that might fail
.doOnError(throwable -> {
LOGGER.error("Just have error {}", throwable.getMessage());
})
.retry() //retry on any error
.subscribe(System.out::println);
}
}
@nkonev
Copy link
Author

nkonev commented Oct 13, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment