2

I need to throw a specific exception in the case if the multi stream doesn't return anything (empty / null in terms of multi stream result);

Here is my repository method:

return reactiveRepository.findAllById(idList);

which returns a

Multi<WhateverModel>

Is there a way for me to throw an exception if the above repository method doesn't return anything / null / empty stream?

I've tried filter, but it doesn't get invoked if the underlying repo method doesn't return a result.

Deniss M.
  • 3,617
  • 17
  • 52
  • 100

1 Answers1

1

You can check if the streams was empty on completion:

stream.onCompletion().ifEmpty().continueWith("a", "b", "c");

That's probably what you need in your case. You can also check that you have received items before a timeout:

stream.ifNoItem().after(timeout)
    .recoverWithMulti(Multi.createFrom().items("a", "b", "c"))

However, not that Multi may not be what you are looking for here. It is often better to use Uni<List<?>> when dealing with a relational database, as their protocol does not support streaming, and, basically, it requires emulating streams in the application by keeping a connection opened.

Clement
  • 2,817
  • 1
  • 12
  • 11
  • Thank you Clement! I am not using a RDB but mongo. Is Multi still a good choice? – Deniss M. May 18 '22 at 12:17
  • 1
    Ah sorry. Yes, Mongo is better. Streams are implemented using batches. It's not ideal (it keeps the connection open) but it is "less bad" than with most relational databases. – Clement May 19 '22 at 07:45