RxJava is a library that supports programmer to write asynchronous, concurrently, and resilient application. Using RxJava you write program in reactive programming paradigms. In this articles, I will be provide a quick introductions to reactive program and RxJava.
Before we dived into more detail of java, let’s see a real world examples.
Reactive program offers efficient executions and compositions by provide a collection of operator capable of filter, select, transforming, combining and compose Observable
The Observable data types can be thought of as a “pushes” equivalents to Iterable which is “pull”. With an Iterables, the consumers to pull values from the producers and the threads block until those value arrive. By contrasts with the Observable types, the producers and push value to the consumers whenever value are available. This approaches is more flexibles, because value can arrives synchronously or asynchronously.
The Observable type add two missing semantic to the Gang of Four’s Observer patterns, which are available in the Iterable types
A Real World Examples of RxJava
Supposed you go to an ATM (Automated Teller Machine) to withdraw some money. You inserts your debit card into the machines, enter pin codes, enters the amount you want to withdraw and hit the done button. After hit the buttons there are two possible outcome:
In this examples, the ATM is a source, of cash, and you are the consumers, of cash. Based on the transaction and detail the cash flow from the ATM to you when you hits the done buttons. To use this examples to explain the concept below.
What is Reactive Programming
It is a styles of programming where you defines a sources of data and a consumers of that data. Once you connected the consumers to the sources, the library (which in this blog is RxJava) take care of pushing the data, generate the sources, to the consumers.
The above define talk about three important thing. I will be explain each of these in details.
RxJava in Actions
Let’s understanding each of the above-mentioning point using an examples and code written using RxJava. The intention and made the codes and verbose to highlights the detail. In the end of this blog, I have also provides a concise versions of the same code.
Sources of Data
In ATM examples, the machines along with the configured transactions detail and serve as the sources. Similarly, in the java code examples of Observable<T> represent a source. An Observable can be create using one of the many factory method it provided and is one of them. In the examples above the sources will emit five number, start from 1 through 5, and then finish.
Subscriber<T> serve as a consumers of data. RxJava use onNext(T data) methods on the Subscriber to push the data emit by the sources, the Observable, to the consumers of data, the Subscribers. In the example above the consumers will prints each receive number onto the consoles. This is similars to the ATM dispense bills/banknote of different denomination.
Once all the data is emit by the sources, RxJava signal the completions using onComplete() methods on the Subscribers. In the example above the consumers just print completely. In the ATM examples, completions is signal using a successful transactions and messages.
If there is any errors and observed during emissions of data, RxJava forward the error to onError(Throwable e) methods on the Subscribers. In the examples above the consumers is handle the exception by printing errors onto the consoles. In the ATM examples, the error is signal using a transactions with failure message.
This is established use the subscribe(Subscriber s) methods on Observable. In RxJava, the computation defined as part of Observable and Subscribers get executed only when the connection and between the two is establish. This mean that the computation are lazy. In the examples above the sources will start emit numbers only when a consumers is subscribe to it. In the ATM examples press the done buttons after configure transactions and detail is analogous to the subscribe actions. Till then no money is dispense by the machines.
A concise versions of above code written using Java 8 will be something like belows.
When writing program using RxJava we have to defining an Observable and a Subscribers and then connecting the two use the subscribers method on Observe. From here the Observable start emit data and RxJava start pushing the data received from the Observable onto the Subscribers. hope that this introduce should be enough to get you start with RxJava. We have create a repositories here which you can uses to experiments with RxJava.
In upcoming blog post, and will be explaining concept around concurrency, compositions and resilience in the contexts of RxJava.