Reactor API — Operator

Pramuditya Ananta Nur
3 min readDec 24, 2021
Photo by Mike U on Unsplash

In this article, I‘ll share some APIs in reactor projects that I often use and are very helpful during reactive project development.

Before continuing to read this article, I hope you are familiar with the concepts of reactive systems and project reactors.

1. Map

Map in the reactor project is used to transform item sent from the publisher to another item using synchronous function.

Map Example

In example above, I do the transformation of the string hello to upper case.

Map Result

2. FlatMap

Flatmap in reactor project is used to transform item into publisher form, it can be Mono or Flux, and flatten the item into the resulting form.

FlatMap Example

The example above is the same as before. However, the parameters parsed into the flatmap can only type of publisher (Mono, Flux, etc).

FlatMap Result

3. FlatMap Parallel

A parallel flatmap is the same as flatmap, the difference is that we can transform the items in parallel according to our needs.

FlatMap Parallel Example

The only thing that needs to be added is how many parallel processes are running by calling the parallel(2) method and don’t forget to call the runOn(Schedulers.parallel()) method.

FlatMap Parallel Result

4. Concat

Concat is used to combine several publishers sequentially, which means the first publisher will be sent first until completion, and then the next publisher will be processed.

Concat Example
Concat Result

There is also an API for delaying when there is an error using concat: ConcatDelayError. Where all publishers will be processed first, and when there is an error will be processed at the end.

ConcatDelayError Example
ConcatDelayError Result

5. Merge

Merge is to combine publishers the same as with concat, but the merger of the publishers does not need to wait for the first publisher to complete. It can be said that merge will emit data simultaneously.

Merge Example

It can be seen in the example above that publisher source 2 with a value of 6 can be processed directly without waiting for publisher source 1 to finish emitting data.

6. Zip

zip function is almost the same as merge. The difference is the data will be merged into a combined object of publisher 1 and publisher 2 using monad (Tuple).

Zip Example

values ​​from publisher source 1 and source 2 will be combined into a merged object (tuple).

Zip Result

What needs to be emphasized is that zip only works when the two publishers combined are still emitting data. If one publisher finishes emitting data and another publisher is still emitting data, then the process will stop when one of the publishers finishes emitting data.

Zip Different Publisher Example

It can be seen in the example above that publisher source 2 still emits data 10 times, while publisher source 1 only does up to 5 times. Then the process will stop when publisher source 1 finishes emitting data.

Zip Different Publisher Example

For now, that’s for some reactor API projects that I often use. For more APIs, I will explain in the next part. See ya.

--

--