/
_07FluxGenerateCounter.java
37 lines (30 loc) · 1.46 KB
/
_07FluxGenerateCounter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.github.adityagarde.reactor.sec03;
import com.github.adityagarde.reactor.util.Util;
import reactor.core.publisher.Flux;
public class _07FluxGenerateCounter {
public static void main(String[] args) {
/*
* New requirement is to Stop emitting after receiving India or after receiving 10 items, which ever is the earliest.
*/
/*
* The problem with `generate(Consumer<SynchronousSink<T>> generator)` implementation is that we cannot have
* a counter variable inside the block as with `generate()` the complete code block is invoked
* and thus the counter will also be reset to zero on every call.
*
* We have a separate implementation `generate(Callable<S> stateSupplier, BiFunction<S, SynchronousSink<T>, S> generator)`
* which accepts a Callable stateSupplier i.e. the initial state and a BiFunction taking the state (counter) and
* SynchronousSink and returning the new state (counter) back.
*/
Flux.generate(
() -> 1,
(counter, synchronousSink) -> {
String country = Util.faker().country().name();
synchronousSink.next(country);
if (counter >= 10 || country.equalsIgnoreCase("India")) {
synchronousSink.complete();
}
return counter + 1;
}
).subscribe(Util.subscriber());
}
}