liveBook · Manning

Buffer

As explained in Spring in Action, Sixth Edition

buffer is a Project Reactor operator that groups source elements into collections of a specified size or according to a buffering condition.

`buffer(3)` breaks a `Flux` into lists of at most three elements. The result is another `Flux`, which emits those lists instead of individual elements. section 11.3.3

Group five fruits

For `Flux.just("apple", "orange", "banana", "kiwi", "strawberry")`, calling `.buffer(3)` produces two lists: `[apple, orange, banana]` and `[kiwi, strawberry]`. `StepVerifier` can check both lists and completion. section 11.3.3

To process chunks in parallel, follow `buffer(3)` with `flatMap()`. Convert each list back to a `Flux`, transform its elements with `map()`, and use `subscribeOn(Schedulers.parallel())` for each resulting stream. `log()` makes their activity visible. section 11.3.3

@Test
public void bufferAndFlatMap() throws Exception {
  Flux.just(
      "apple", "orange", "banana", "kiwi", "strawberry")
      .buffer(3)
      .flatMap(x -> 
        Flux.fromIterable(x)
          .map(y -> y.toUpperCase())
          .subscribeOn(Schedulers.parallel())   
          .log()
      ).subscribe();
}

The complete fruit-stream example shows how buffering feeds parallel transformations. section 11.3.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Works through sized lists and a parallel-processing pipeline; does not explain buffering internals.
Examples
Uses five fruit names to demonstrate two buffers and parallel uppercase transformations.
What you can do after
Apply it via Project Reactor to group Flux elements and process each group in parallel.
How it connects
Connects buffer to Flux, flatMap, map, subscribeOn, and StepVerifier.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.