Never Vs Empty
When we call Mono.never()
or Flux.never()
, the stream will never signal to complete and runs forever. In constrast to Mono.empty()
and Flux.empty()
which will complete but just signal nothing.
Consider the following example:
package reactor.learn;
import reactor.core.publisher.Mono;
public class MonoApp {
public static void main(String[] args) {
// emptyMonoMain();
noSignalMonoMain();
}
public static Mono<String> emptyMono() {
return Mono.empty();
}
public static Mono<String> monoNoSignal() {
return Mono.never();
}
public static void noSignalMonoMain() {
monoNoSignal() // runs forever
.doOnSuccess((result) -> System.out.println("Finish")) // Would not print
.subscribe((result) -> System.out.println("Done: " + result)); // Would not print
}
public static void emptyMonoMain() {
emptyMono()
.doOnSuccess((result) -> System.out.println("Finish"))
.subscribe((result) -> System.out.println("Done: " + result)); // Would not print
}
}
The Mono.empty()
will print Finish
but the never()
will not.