Skip to content

Getting started with a simple example

kewangie edited this page Feb 19, 2015 · 10 revisions

Let's process a collection of data

Starting with a collection of words we will count the letters of each 'uncorrupted' word. And let's assume all the corrupted words, very handily, start with '$'.

List<String> list = Arrays.asList("hello","world","$da^","along","$orrupted");

Set up a reactive dataflow

new SimpleReact().reactToCollection(list)

Let's modify it to filter out corrupt words

(assume they all start with $)

new SimpleReact().reactToCollection(list)
.filter(it -> !it.startsWith("$"))

Now let's convert each word left in the flow into it's size.

The then method (inspired by the promises spec then method) behaves a bit like the map method in the JDK Streams api. Converting an input into an output. For those familar with the Javascript promises spec - we've broken out then into multiple parts - failure handling is managed by the onFail and capture methods.

new SimpleReact().reactToCollection(list)
    .filter(it -> !it.startsWith("$"))
    .then(it -> it.length())

At this point we would get back a collection of [5,5,5] if we called block. The block() method, blocks the current thread until the reactive flow has completed - returning all results (look for overloaded block methods to return only a selection of results).

Now let's sum the word lengths.

We can call block and use the JDK 8 Streams api to sum them.

int count  = new SimpleReact()
	.reactToCollection(list)
	.filter(it -> !it.startsWith("$"))
	.then(it -> it.length())
	.block().stream().reduce(0, (acc,next) -> acc+next );

assertThat(count, is(15));

Use peek to debug the flow

 int count  = new SimpleReact()
	.reactToCollection(list)
	.filter(it -> !it.startsWith("$"))
	.peek(it -> System.out.println(it))  // hello,world,along
	.then(it -> it.length())
	.peek(it -> System.out.println(it))  //5,5,5 [e.g. hello.length()==5] 
	.block().stream().reduce(0, (acc,next) -> acc+next );

Use capture to log errors

List list = Arrays.asList("hello","world","$da^","along","$orrupted",null);

	List<String> list = Arrays.asList("hello","world","$da^","along",null);
	int count  = new SimpleReact()
			.reactToCollection(list)
			.capture(e -> e.printStackTrace())
			.filter(it -> !it.startsWith("$"))
			.then(it -> it.length())
			.block().stream().reduce(0, (acc,next) -> acc+next );

	assertThat(count, is(15));

Note a StrackTrace is output when this code is run.

Use onFail to recover from errors

	List<String> list = Arrays.asList("hello","world","$da^","along","$orrupted",null);
	int count  = new SimpleReact()
			.reactToCollection(list)
			.capture(e -> e.printStackTrace())
			.filter(it -> !it.startsWith("$"))
		
			.onFail(e -> { if(e.getCause() instanceof NullPointerException) { return "null"; } return "";})

			.then(it -> it.length())
			.block().stream().reduce(0, (acc,next) -> acc+next );

	assertThat(count, is(19));

Note that no StackTrace is output when this code is run, as the exception has been handled.

Clone this wiki locally