Reactor API — Operator
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.
In example above, I do the transformation of the string hello to upper case.
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.
The example above is the same as before. However, the parameters parsed into the flatmap can only type of publisher (Mono, Flux, etc).
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.
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.
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.
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.
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.
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).
values from publisher source 1 and source 2 will be combined into a merged object (tuple).
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.
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.
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.