0

In my project I have this:

ParallelFlux<Device> flux = Flux.fromIterable(children)
                .delayElements(Duration.ofMillis(10))
                .parallel(18)
                .runOn(Schedulers.elastic(), 10)
                .doOnNext(c -> recursiveValidationThroughoutChildren(c, tracker)
                });

Where recursiveValidationThroughoutChildren is a method with this declaration:

boolean recursiveValidationThroughoutChildren(Device d, NodeChangesTracker tracker) throws Exception;

What I don't understand is how to handle the exception thrown by this last method. I would like the exception to be propagated outside the ParallelFlux. Is it possible? What is the correct way to handle it?

Stefania
  • 587
  • 1
  • 10
  • 26
  • I believe you should be able to find an answer to your question here: https://stackoverflow.com/questions/53595420/correct-way-of-throwing-exceptions-with-reactor – Rozart Oct 17 '19 at 15:40
  • I'd agree with @Rozart - but if not, let us know why it doesn't solve it so we can avoid closing it as a dupe :-) – Michael Berry Oct 17 '19 at 20:22

1 Answers1

0

I followed the link @Rozart suggested, but I could not apply the solution as it is explained. I had to change it a bit:

ParallelFlux<Device> flux = Flux.fromIterable(children)
                .delayElements(Duration.ofMillis(10))
                .parallel(18)
                .runOn(Schedulers.elastic(), 10)
                .doOnNext(child -> {
                    try {
                        recursiveValidationThroughoutChildren(child, tracker);
                    } catch (Exception ex) {
                        Flux.error(ex);
                    }
                });

The change is needed because the ParallelFlux does not support the "handle" method, so I had to add a try catch and relaunch the exception with a Flux.error. I don't know if it is good practice, but it is the only way I got it work.

Stefania
  • 587
  • 1
  • 10
  • 26