Mono is a type of instant Publisher that represents a single or empty value. This means it can emit only one value at most for the onNext() request and then terminates with the onComplete() signal. In case of failure, it only emits a single onError() signal.Most Mono implementations are expected to immediately call onComplete on their Subscriber after having called onNext.a combination of onNext and onError is explicitly forbidden.
Overridden Lambda implementation available in mono
subscribe(); //1 subscribe(Consumer<? super T> consumer); //2 subscribe(Consumer<? super T> consumer, Consumer<? super Throwable> errorConsumer); //3 subscribe(Consumer<? super T> consumer, Consumer<? super Throwable> errorConsumer, Runnable completeConsumer); //4 subscribe(Consumer<? super T> consumer, Consumer<? super Throwable> errorConsumer, Runnable completeConsumer, Consumer<? super Subscription> subscriptionConsumer); //5
//1
Publisher<String> rctMono = Mono.just("Hello React"); // Simple Mono Publisher using Just var subs = new SubscriberImpl(); rctMono.subscribe(subs); subs.getSubscription().request(10);
Output
08:21:46.241 [main] INFO org.mugil.subscriber.SubscriberImpl -- Received Hello React 08:21:46.252 [main] INFO org.mugil.subscriber.SubscriberImpl -- Subscription ended
//5
Mono<Integer> rctMono2 = Mono.just(1) .map(i -> i/0); rctMono2.subscribe(i -> System.out.println(i), //Consumer err -> System.out.println("Error Msg -" + err.getMessage()), //onError, Not Mandatory () -> System.out.println("Completed"), //onComplete, Not Mandatory subscription -> subscription.request(1)); //onRequest, Not Mandatory
Output
Error Msg -/ by zero
Simple code which returns Mono based on switch case
public static Mono<String> getUserName(Integer num){ return switch (num){ case 1 -> Mono.just("How are you"); case 2 -> Mono.empty(); default -> Mono.error(new RuntimeException("Invalid Input")); }; }
Using mono.just would invoke the sumOfNums as it always fetches value from JVM memory rather than streaming
public static void main(String[] args) { List<Integer> lstNums = List.of(1,2,3); Mono.just(sumOfNums(lstNums)); //Mono.just takes the value from memory so wont be suitable for streaming data incase large data should be handled } public static int sumOfNums(List<Integer> lstNums){ System.out.println("Sum invoked"); return lstNums.stream().mapToInt(num -> num).sum(); }
Output
Sum invoked
Using mono.fromSupplier would invoke the sumOfNums during Terminal Operation rather than Intermediate Operation
List<Integer> lstNums = List.of(1,2,3); Mono.fromSupplier(() -> sumOfNums(lstNums)); // Intermediate Operation public static int sumOfNums(List<Integer> lstNums){ System.out.println("Sum invoked"); return lstNums.stream().mapToInt(num -> num).sum(); }
Output
Mono.fromSupplier vs Mono.fromCallable
fromCallable calls a checked exception where as fromSupplier doesnot throws checked exception. So if you substitute supplier in place of callable you should also write try catch block to handle exception