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.

Project Reactor’s buffer() groups elements from a Flux into lists, producing a new Flux of those lists. With buffer(3), each list contains at most three elements; a final list can be smaller. section 11.3.3

@Test
public void buffer() {
  Flux<String> fruitFlux = Flux.just(
      "apple", "orange", "banana", "kiwi", "strawberry");
  
  Flux<List<String>> bufferedFlux = fruitFlux.buffer(3);
  
  StepVerifier
      .create(bufferedFlux)
      .expectNext(Arrays.asList("apple", "orange", "banana"))
      .expectNext(Arrays.asList("kiwi", "strawberry"))
      .verifyComplete();
}

Five fruit names become two lists: the first contains three names, and the second contains the remaining two. StepVerifier checks both lists and completion. section 11.3.3

Buffering can also prepare chunks for parallel processing. Use flatMap() to turn each list back into a Flux, map its elements, and schedule each sub-Flux with subscribeOn(Schedulers.parallel()). Buffering alone does not make the work parallel. 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();
}

This example uppercases the fruit names on parallel scheduler threads and uses log() to show Reactive Streams events. section 11.3.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains size-limited buffering and parallel processing of buffers with a worked example.
Examples
Uses five fruit names to demonstrate two buffers and uppercase processing on parallel threads.
What you can do after
Apply it via Project Reactor to group stream elements and schedule buffered work in parallel.
How it connects
Connects buffering with Flux, flatMap(), map(), parallel schedulers, 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.