Flux

Flux represents an asynchronous sequence of 0 to n items.

We will again borrow a diagram from the Project Reactor documentation, which explains how a Flux object emits items:

Item emitted by a Flux object

The preceding diagram illustrates the following process:

  • At least six values have been produced
  • An operator is applied to the produced values
  • The result is delivered

In the following example, we will first convert each produced value to uppercase, in order to deliver the values:

@Test
public void givenAListOfCapitalizedStrings_WhenThe
FlatMapConvertsToUpperCaseTheStrings_ThenTheStringsAre
InUpperCase() throws Exception
{
List<String> namesCapitalized = Arrays.asList("John",
"Steve", "Rene");
Iterator<String> namesCapitalizedIterator = namesCapitalized.
iterator();
Flux<String> fluxWithNamesCapitalized = Flux.fromIterable
(namesCapitalized);
Flux<String> fluxWithNamesInUpperCase = fluxWithNamesCapitalized
.map(name -> name.toUpperCase());
fluxWithNamesInUpperCase.subscribe
(
nameInUpperCase ->
{
String expectedString =namesCapitalizedIterator.
next().toUpperCase();
Assert.assertEquals(expectedString, nameInUpperCase);
}
);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.239.103