O'Reilly logo

Hands-On Reactive Programming with Reactor by Rahul Sharma

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The buffer operator

The buffer() operator gathers all Flux<T> elements and emits them as a List<T>. Unlike groups generated by the groupBy() operator, all the elements in the List<T> buffer are in their original order. Alternatively, we could provide a batchSize to the operator. The operator will then generate N lists, each of which will have a specified number of elements. Let's try to use the buffer operator on our Fibonacci series:

    @Test    public  void testBufferWithDefinateSize(){        Flux<Long> fibonacciGenerator = Flux.generate(() -> Tuples.<Long,                Long>of(0L, 1L), (state, sink) -> {            if (state.getT1() < 0)                sink.complete();            else                sink.next(state.getT1());            return Tuples.of(state.getT2(), state.getT1() + state.getT2());        }); fibonacciGenerator.take(100) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required