Operators

The functions used to manipulate Flux are usually the operators, as discussed here:

  • log(): This is used to log to the standard output as follows:
Flux<String> flux = 
Flux.just("mango","strawberry","pineapple","papaya").log();
  • map(): This is used to transform the streams, as follows:
Flux<String> flux = 
Flux.just("mango","strawberry","pineapple",
"papaya").log().map(data-> data.toUpperCase());

We called operators on Flux, however, these operators are not getting executed now; it's actually just a plan of execution. The following figure gives us clarity on how the operators work on the streams. Remember the result is subject to change as per the operator used on the stream:

The data will begin to flow only when someone subscribes to it. All these operators are declarative, which facilitates the developers to concentrate on the logic. It's usually called functional programming as well. It can even be used to process Java 8 Streams as follows:

Stream<String> stream = 
Streams.of("mango","strawberry","pineapple","papaya"); Stream<String> upper = stream.map(value -> { System.out.println(value); return value.toUpperCase(); });

Some of the operators provided by Flux are as follows:

  • Flux<T> empty(): This creates Flux, which gets completed without emitting any elements
  • Flux<T> concat(): This concats all the sources that are pulled from the iterator, publisher, or emitter
  • Flux<T> create(): This creates Flux that has the capability of multiple emissions
  • Flux<T> delay(): This signals to the onNext() method of subscriber to delay Flux until the given period is not elapsed
  • Flux<Long> interval(): This creates Flux that emits the data of type Long for the time period
  • Flux<T> just: This creates a new Flux that emits the specified items
  • Flux<T> merge(): This merges the emitted publisher sequences

Some of the operators that are supported by Mono are as follows:

  • Mono<T> create(): This creates a deferred emitter that can be used with callback-based APIs
  • Mono<Tuple2<T,T2>> and(): This combines the result from two Monos in a Tuple2, out of which, one Mono is this
  • Mono<T> empty(): This creates a Mono that will get completed without emitting any item
  • Mono<T> otherwise() : (): This gets unsubscribed to a returned publisher when any error that is matching with the given type occurs.
..................Content has been hidden....................

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