{"id":5291,"date":"2024-10-19T03:08:20","date_gmt":"2024-10-19T03:08:20","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=5291"},"modified":"2024-10-19T07:26:02","modified_gmt":"2024-10-19T07:26:02","slug":"mono-subscriber","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/mono-subscriber\/","title":{"rendered":"Mono Subscriber"},"content":{"rendered":"<p>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.<\/p>\n<p><strong>Overridden Lambda implementation available in mono<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nsubscribe();  \/\/1\r\n\r\nsubscribe(Consumer&lt;? super T&gt; consumer);  \/\/2\r\n\r\nsubscribe(Consumer&lt;? super T&gt; consumer,\r\n          Consumer&lt;? super Throwable&gt; errorConsumer);  \/\/3\r\n\r\nsubscribe(Consumer&lt;? super T&gt; consumer,\r\n          Consumer&lt;? super Throwable&gt; errorConsumer,\r\n          Runnable completeConsumer);  \/\/4\r\n\r\nsubscribe(Consumer&lt;? super T&gt; consumer,\r\n          Consumer&lt;? super Throwable&gt; errorConsumer,\r\n          Runnable completeConsumer,\r\n          Consumer&lt;? super Subscription&gt; subscriptionConsumer); \/\/5\r\n<\/pre>\n<p><strong>\/\/1<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n        Publisher&lt;String&gt; rctMono  = Mono.just(&quot;Hello React&quot;); \/\/ Simple Mono Publisher using Just\r\n        var subs = new SubscriberImpl();\r\n        rctMono.subscribe(subs);\r\n        subs.getSubscription().request(10);\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n08:21:46.241 [main] INFO org.mugil.subscriber.SubscriberImpl -- Received Hello React\r\n08:21:46.252 [main] INFO org.mugil.subscriber.SubscriberImpl -- Subscription ended\r\n<\/pre>\n<p><strong>\/\/5<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\n       Mono&lt;Integer&gt; rctMono2 = Mono.just(1)\r\n                                    .map(i -&gt; i\/0);\r\n       rctMono2.subscribe(i -&gt; System.out.println(i),   \/\/Consumer\r\n                          err -&gt; System.out.println(&quot;Error Msg -&quot; + err.getMessage()), \/\/onError, Not Mandatory\r\n                          () -&gt; System.out.println(&quot;Completed&quot;), \/\/onComplete, Not Mandatory\r\n                          subscription -&gt; subscription.request(1)); \/\/onRequest, Not Mandatory\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nError Msg -\/ by zero\r\n<\/pre>\n<p><strong>Simple code which returns Mono based on switch case<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n    public static Mono&lt;String&gt; getUserName(Integer num){\r\n        return switch (num){\r\n            case 1 -&gt; Mono.just(&quot;How are you&quot;);\r\n            case 2 -&gt; Mono.empty();\r\n            default -&gt; Mono.error(new RuntimeException(&quot;Invalid Input&quot;));\r\n        };\r\n    }\r\n<\/pre>\n<p><strong>Using mono.just would invoke the sumOfNums as it always fetches value from JVM memory rather than streaming<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static void main(String&#x5B;] args) {\r\n  List&lt;Integer&gt; lstNums = List.of(1,2,3);\r\n  Mono.just(sumOfNums(lstNums)); \/\/Mono.just takes the value from memory so wont be suitable for streaming data incase large data should be handled\r\n}\r\n\r\npublic static int sumOfNums(List&lt;Integer&gt; lstNums){\r\n   System.out.println(&quot;Sum invoked&quot;);\r\n   return lstNums.stream().mapToInt(num -&gt; num).sum();\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nSum invoked\r\n<\/pre>\n<p><strong>Using mono.fromSupplier would invoke the sumOfNums during Terminal Operation rather than Intermediate Operation<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nList&lt;Integer&gt; lstNums = List.of(1,2,3);\r\n        Mono.fromSupplier(() -&gt; sumOfNums(lstNums)); \/\/ Intermediate Operation                 \r\n\r\n\r\n  public static int sumOfNums(List&lt;Integer&gt; lstNums){\r\n        System.out.println(&quot;Sum invoked&quot;);\r\n        return lstNums.stream().mapToInt(num -&gt; num).sum();\r\n  }\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n<\/pre>\n<p><strong>Mono.fromSupplier vs Mono.fromCallable<\/strong><br \/>\nfromCallable 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<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip; <a href=\"https:\/\/codethataint.com\/blog\/mono-subscriber\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[369],"tags":[],"class_list":["post-5291","post","type-post","status-publish","format-standard","hentry","category-reactive-programming"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=5291"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5291\/revisions"}],"predecessor-version":[{"id":5296,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5291\/revisions\/5296"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=5291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=5291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=5291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}