October 2018
Intermediate to advanced
556 pages
15h 18m
English
The generate factory method is designed to allow for the creation of complicated sequences based on an internal forwarded state of the generator. It requires an initial value and a function, which calculates the next internal state based on the previous one and also sends the onNext signal to a downstream subscriber. For example, let's create a simple Reactive Stream that produces the Fibonacci sequence (1, 1, 2, 3, 5, 8, 13, ...). The code for this task may look as follows:
Flux.generate( // (1) () -> Tuples.of(0L, 1L), // (1.1) (state, sink) -> { // log.info("generated value: {}", state.getT2()); // sink.next(state.getT2()); // (1.2) long newValue = state.getT1() + state.getT2(); // return Tuples.of(state.getT2(), ...