Over a million developers have joined DZone.

RxJava: Reactive Extensions for Java

A look at how Observables and Observers work together in RxJava, the implementation of ReactiveX for Java.

· Java Zone

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

RxJava is the Java implementation of ReactiveX, and is a library for composing asynchronous and event-based programs using observable sequence for the JVM.

RxJava implements the Reactor and observer patterns that could be considered a combination of Interator Pattern and Functional Programing. In other words, they are a lot of functions to create, combine, and filter streams.

According to the official documentation: 

"It extends the observer pattern to support sequences of data and/or events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety, concurrent data structures, and non-blocking I/O."

Image title

The main target of RxJava is the whole JVM platform, not just one language. Right now, it supports Java, Groovy, Scala, JavaScript, and Ruby.

One of the ideas of RxJava is provide a DSL for creating flows out of asynchronous sources using collections of operators for filtering, selecting, transforming, and combining those flows in an easy way. It encapsulates data sequences in Observable and performs composable operations on them.

These flows are called Observables, which are collections of events with push semantics, opposed to pull semantics used with iterators.

Non-blocking is not about being faster, non-blocking is more about utilizing threads better. If thread utilization is not a issue, the application will perform at about the same level.

Some facts on Observers and Observables:

  • The Observable is the source of a data stream (sender).
  • The Observer is the listener for emitted values (receiver).
  • The Observer subscribes (listens) to the Observable.
  • Observers react to whatever item or sequence of items the Observable emits.
  • Many observers can subscribe to the same observable.

Image title

Some differences between the Observable and Observer Patterns:

  • Observer

    • Allows for concurrent operations
    • Does not need to block threads while waiting for the observable to emit values
    • Observer waits to receive values when the observable is ready to emit them
    • Based on push rather than pull
  • Observable

    • Composable and easily chained together or combined
    • Flexible and can be used to emit network results, sequences, or infinite streams
    • Free from callback hell
    • Easy to transform one asynchronous stream into another

Image title

Sample Code

Observable.just("Hello, world!")
    .map(s -> s.hashCode())
    .map(i -> Integer.toString(i))
    .subscribe(s -> System.out.println(s));

Comparison Between Iterable and Observable Architecture

 Iterable Architecture — How it Works

  • Call a method
  • Wait for result
  • Store the return value from that method in a variable
  • Use that variable and its new value to do something useful

 Observable Architecture — How it Works

  • Define an Observer that specifics what to do with each emitted value
  • Call a method that returns an Observable
  • Subscribe the Observer to the Observable
  • Tell the Observable that it has a subscriber waiting to receive values when they're available

The subscribe method connects an Observer to an Observable, the subscriber does not need to block the thread, and the values will come to your Observer when they are ready.

What should we think about when we use RxJava:

  • Treat all data as immutable
  • Since we treat data as a stream, we can return one or more results for each subscription
  • We can transform and combine data on any thread and subscribe to update for related values or collections

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:
java ,rxjava ,stream

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}