Reactive programming with Java 8 and simple-react : getting started

John McClean
2 min readMar 24, 2015

--

The easiest way to get started with simple-react is to simply create an instance of SimpleReact and go from there.

The SimpleReact stream is the smallest and most focused Stream API in simple-react — making it an excellent starting point for Reactive programming.

A very simple, getting started example, might be to try to add 100 to a series of numbers -

new SimpleReact().of(1,2,3,4,5)
.then(num -> num+100)
.peek(System.out::println);

And you might well wonder what was the advantage of using SimpleReact to do that? (Did you notice the position of 105?)

Well, one of the advantages is that everything (nearly) happened asynchronously, let’s make that clearer with some code

new SimpleReact().of(1,2,3,4,5)
.then(num -> num+100)
.then(num -> Thread.currentThread().getId())
.peek(System.out::println);

Now we’re going to output the thread id instead of the total — watch

Each of our tasks at a phase in the Stream were executed on separate threads!

react!

Our input to the Stream was a standard java array (via vararg parameters), we could also start the Stream asynchronously via the react command. The react command accepts an array of Suppliers. Supplier is a java 8 functional interface that returns a value. simple-react will call the Suppliers aysnchronously via an ExecutorService.

new SimpleReact().react( ()-> readData(“data1"),
()-> readData(“data2"))

This will eagerly and asynchronously call the readData method. We can build on this and chain events and responses.

peek!

new SimpleReact().react( ()-> readData(“data1"),
()-> readData(“data2"))
.peek(System.out::println);

peek will allow us to see what is happening at a given stage. It accepts a Consumer as a parameter. A consumer is a JDK 8 functional interface that takes one parameter but doesn’t return anything.

then!

then allows us to transform data at a given phase. It accepts a function as a parameter. A function is a JDK 8 functional interface that accepts one parameter and returns a value. Here we are using method references, where this.processData(input) matches the parameter and return type signature of Function.

new SimpleReact().react( ()-> readData(“data1"),
()-> readData(“data2"))
.peek(System.out::println)
.then(this::processData);

block!

block not only blocks the current thread until all jobs in the Stream have completed, but also allows us to capture the results

List<String> results =new SimpleReact()
.react( ()-> readData(“data1"),
()-> readData(“data2"))
.peek(System.out::println)
.then(this::processData)
.block();

onFail!

onFail allows us to recover from errors, either specific or general

List<String> results =new SimpleReact()
.react( ()-> readData(“data1"),
()-> readData(“data2"))
.onFail(FileNotFoundException.class,
this::loadFromDb)
.peek(System.out::println)
.then(this::processData)
.block();

And to put it all together!

To add simple-react as a depencency to your project see —

The Tutorial : Reactive programming with Java 8

--

--

John McClean

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