Skip to Content
Hands-On Reactive Programming in Spring 5
book

Hands-On Reactive Programming in Spring 5

by Oleh Dokuka, Igor Lozynskyi
October 2018
Intermediate to advanced
556 pages
15h 18m
English
Packt Publishing
Content preview from Hands-On Reactive Programming in Spring 5

Factory method – generate

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(), ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Spring Microservices in Action, Second Edition

Spring Microservices in Action, Second Edition

John Carnell, Illary Huaylupo Sanchez

Publisher Resources

ISBN: 9781787284951Supplemental Content