At some point in our code using rxjava we have
public Maybe<Event> verifyFoo(Event event){ return Maybe.just(event) .flatMap(this::foo) .doOnComplete(() -> logNotFound(event)); } private Maybe<Event> foo(Event event){ repository.findFooOrEmpty(event). .map(unused -> event); } private void logNotFound(Event event){ logger.warn("Foo not found for bar: ", event.getBar()); } Basically we want to know if something is in the repository, if it is keep going, if not do some side effect and return Maybe empty to stop the flow.
We are studying changing to reactor and to my surprise, Mono does not have doOnComplete. The closest thing I have found would be something like this:
public Mono<Event> verifyFoo(Event event){ return Mono.just(event) .flatMap(this::foo) .switchIfEmpty(Mono.defer(() -> logNotFound(priceEvent))); } private Mono<Event> foo(Event event){ repository.findFooOrEmpty(event). .map(unused -> event); } private Mono<Event> logNotFound(Event event){ logger.warn("Foo not found for bar: ", event.getBar()); return Mono.empty(); } But it doesn't feel right. So what would be a proper substitution for this case?