liveBook · Manning

Flux

As explained in Spring in Action, Sixth Edition

Flux is Project Reactor's publisher type for asynchronously emitting zero or more ordered elements.

Flux is Project Reactor’s publisher type for asynchronously emitting zero or more ordered elements, including potentially infinite streams. It implements Reactive Streams’ Publisher and forms a pipeline through which data flows without assuming that every operation runs on the same thread. section 11.2

Create Flux instances, transform their values, combine streams, and subscribe to start receiving data. Mono handles datasets containing no more than one element; Flux handles broader streams and shares many of Mono’s operations. section 11.2 section 11.3

Common Flux composition patterns include:

  • Merge streams with mergeWith() so values arrive according to source timing rather than a guaranteed alternating order. section 11.3.2
  • Zip streams so each output combines corresponding elements from both sources, either as tuples or transformed messages. section 11.3.2
  • Select the first source to emit with firstWithSignal(), then continue receiving values from that source. section 11.3.2

Merge character and food streams

A character Flux containing Garfield, Kojak, and Barbossa can be merged with a food Flux containing Lasagna, Lollipops, and Apples. Delays make the emissions interleave, and StepVerifier checks the resulting sequence. section 11.3.2

@Test
public void mergeFluxes() {
  
  Flux<String> characterFlux = Flux
      .just("Garfield", "Kojak", "Barbossa")
      .delayElements(Duration.ofMillis(500));
  Flux<String> foodFlux = Flux
      .just("Lasagna", "Lollipops", "Apples")
      .delaySubscription(Duration.ofMillis(250))
      .delayElements(Duration.ofMillis(500));
  
  Flux<String> mergedFlux = characterFlux.mergeWith(foodFlux);
 
  StepVerifier.create(mergedFlux)
      .expectNext("Garfield")
      .expectNext("Lasagna")
      .expectNext("Kojak")
      .expectNext("Lollipops")
      .expectNext("Barbossa")
      .expectNext("Apples")
      .verifyComplete();
}

Shows a complete mergeWith() test, including delayed emissions and StepVerifier assertions. section 11.3.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains Flux fundamentals and selected creation, combination, and transformation operations, but stops short of surveying all available operations.
Examples
Uses delayed character and food streams, mergeWith(), zip(), firstWithSignal(), and StepVerifier tests.
What it assumes
Assumes basic Java and publisher terminology while introducing reactive pipelines, Flux, Mono, and subscription behavior.
What you can do after
Apply it via Project Reactor to build, combine, subscribe to, and test asynchronous data pipelines.
How it connects
Connects Flux with Reactor, Reactive Streams Publisher, Mono, and RxJava or ReactiveX types.

Also explained in

Compare how 3 books explain Flux →

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.