Reactive programming with Java 8 and simple-react : firstOf, allOf & anyOf

firstOf!

John McClean
Mar 25, 2015 · 1 min read

simple-react can take an array of FutureStreams and ‘run a race’ between them. It will return a Stream of values from the first one to start emitting values, the other Streams will be closed.

Example

LazyFutureStream stream1 = 
LazyFutureStream.sequentialBuilder()
.react(()->loadFromDb())
.map(this::convertToStandardFormat)
LazyFutureStream stream2 =
LazyFutureStream.sequentialBuilder()
.react(()->loadFromService1())
.map(this::convertToStandardFormat)
LazyFutureStream stream3 =
LazyFutureStream.sequentialBuilder()
.react(()->loadFromService2())
.map(this::convertToStandardFormat)
LazyFutureStream.firstOf(stream1,stream2,stream3)
.map(this::save)
.runOnCurrent()

anyOf!

LazyFutureStream.parallelBuilder()
.react(() -> loadFromDb(),
() -> loadFromService1(),
() -> loadFromService2())
.anyOf(Function.<String>identity())
.map(this::convertToStandardFormat)
.peek(System.out::println)
.map(this::saveData)
.run(()-> new ArrayList<>());

NB filtering short-circuits the internal chain of CompletableFutures, and may result in an empty completed value.

allOf!

The Tutorial : Reactive programming with Java 8

John McClean

Written by

Architecture @ Verizon Media. Maintainer of Cyclops. Twitter @johnmcclean_ie