How it works...

Both Mono<T> and Flux<T> types are not the same technically as compared to typical data structures like arrays and List because the latter are fixed memory chunks representing the types of data to be stored while the former is a series of elements generated periodically, which cannot be referenced by an initial and last index. The Stream can be unpredictable since it can be full for a certain period and empty during busy transmissions. Every data emission may contain a different set of actual data depending on the backpressure applied to the Stream object .

A Mono<T> publisher generates a Stream of 0 or 1 data objects and consists of threaded operations that execute asynchronously creating layers of internal Streams. It has core operations such as doOnSuccess() which is triggered to execute once all the operations exited without any errors. Its data emission triggers the method doOnNext() which can be appropriate to execute logging or external tasks. In the service method processFormUser(), a Stream of String objects is being manipulated by the map() and filter() operators which are both asynchronous in nature. The threaded filter() requires java.util.function.Predicate to process only those strings with greater than five alphanumeric characters whereas map() executes java.util.function.Function to convert the filtered Stream object to uppercase. If the object does not qualify with filter(), the resulting Stream will return Null. To accept null data, Mono has a method, justOrEmpty() that will pre-empt any instances of NullPointerException triggered by processing Null Streams. Reactive Streams prohibits the use of Null values in any of its operations:

If a series of data is involved, the Flux<T> Stream type is used but it only requires non-null inputs. To generate Flux<T> from raw data, it uses the just() method just like Mono<T> but some of its operators are for multiple-data Streams. A Flux Stream can emit an infinite amount of data using doOnNext() before doOnComplete() or doOnError() is triggered.

To handle java.lang.Error or java.lang.Exception, both Mono<T> and Flux<T> can invoke onErrorReturn() to return a value that represents an error flag, doOnError() to determine the type of exception, and execute Consumer<T> when the transmission error happens or retry()/retryWhen() to signal a re-subscription after a range() of time with delays. To verify the new Subscription object, invoke doOnSubscribe().

Sometimes defaultIfEmpty() is called to return any arbitrary value just to avoid a null value result which is not tolerated by Publisher<T> and Subscriber<T>. And to check as to where along the way the event got a null value, a log() method is present to show all transaction logs for the purpose of auditing and tracing. It shows all the series of onNext() calls to verify the situations during each Stream operation. The method uses the logback framework in generating the log messages.

Some publishers are created for the purpose of creating dummy tests and providing defaults to risky Streams. These Streams without data emissions are created through Mono.empty() and Flux.empty(). And in some rare cases we invoke Mono.never() to create a Mono<T> Stream that does not execute any callback functions such as onComplete() and onError().

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.135.36