Rx Java Basics

Knoldus Blog Audio
Reading Time: 3 minutes

Overview

RxJava is a JVM implementation of Reactive Extensions and a reactive programming implementation based on event driven and asynchronous programs. It works on the principles of observables and observer patterns. RxJava is lightweight and implemented as a single JAR on the observable abstraction and related higher-order functions.

(For those who are new to Reactive Programming and have less familiarity with it. Reactive programming is an event driven programming where data streams comes in asynchronous manner and get processed as they arrive. ReactiveX is a project which aims to provide reactive programming concept to various programming languages.

Reactive Programming refers to the scenario where program reacts as and when data appears. It is a event based programming concept and events can propagate to observers. As per Reactive, they have combined the best of Observer pattern, Iterator pattern and functional pattern. ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming.)

Observable Observer

Figure: Observable and Observer

Main Components

The main components of RxJava are the observables, operators and observers

Observable

Is the source of data. An object similar to stream which can emit zero or more data, can send error message, speed can be controlled while emitting a set of data and can send finite as well as infinite data.

Operators:

The operators are used for transforming the data stream.

Observer

Is the consumer of data. It subscribes to Observable’s data and reacts per item of the observables. Observers are notified whenever Observable emits data. An Observer handles data one by one.

Dependency

To include RxJava into the project runtime, maven config, gradle config or jar file into classpath can be chosen.

Maven Dependency

<dependency>

<groupId>io.reactivex</groupId>

<artifactId>rxjava</artifactId>

<version>${rx.java.version}</version>

</dependency>

Gradle Dependency

compile ‘io.reactivex.rxjava:rxjava:x.y.z’

Observables Base Classes

RxJava features several base classes.

  • Flowable: 0..N flows, supporting Reactive-Streams and backpressure
  • Observable: 0..N flows, no backpressure
  • Single: a flow of exactly 1 item or an error
  • Completable: a flow without items but only a completion or error signal
  • core.Maybe: a flow with no items, exactly one item or an error.

Operators

RxJava operators are categorised as below.

Sample Code

RxJava

The first line is the Observable i.e stream of data. Next is the operator ‘map’ which transforms the observable data. At the end the observer subscribing to the observable and printing the transformed data from the stream.

Uses

The following are some uses of RxJava.

  • It is a simpler way to handle asynchronous work
  • Availability of multitude of operators to make work less complicated
  • Clean Code
  • Simple to create streams of data
  • Ease in implementing back-pressure

References

http://reactivex.io/

Written by 

Rohit Jagati is a technologist, experienced in designing enterprise grade solutions. Rohit has been designing and implementing solutions based on Machine Learning, IOT and custom/bespoke applications.