Landing in the Java world – Netflix RxJava

In 2012, at Netflix, they started to realize that their architecture was having a hard time to properly adapt to the amount of users they had. They decided to redesign their architecture to reduce the number of REST calls. Instead of having dozens of REST calls and letting the client process the data as needed, they decided to create a single optimized REST call based on the clients' needs.

To achieve this goal, they decided to go reactive, and they started to port .NET Rx to the JVM. They didn't want to target just Java; they decided to target the JVM instead, having the possibility to provide a new tool for every JVM language on the market: Java, Closure, Groovy, Scala, and so on.

With a post on the Netflix tech blog in February 2013, Ben Christensen and Jafar Husain showed RxJava to the world for the first time.

The key concepts were:

  • Easy concurrency to better use their server's power
  • Easy conditional asynchronous execution
  • A proper way to escape the callback hell
  • A reactive approach

As for .NET, RxJava Observable is the push equivalent of Iterable, which is pull. The pull approach is a block-and-wait approach: the consumer pulls values from the source, blocking the thread until the producer provides new values.

The push approach works on subscription and reaction: the consumer subscribes to new values' emissions; the producer pushes these new values when they are available, and notifies the consumer. At this point, the consumer, well, consumes them. The push approach is clearly more flexible, because from a logical and practical point of view, the developer can simply ignore if the data he needs comes synchronously or asynchronously; his code will still work.

What's different in RxJava

From a pure Java point of view, the RxJava Observable class extends the classic Gang of Four Observer pattern concept.

It adds three missing abilities:

  • The producer can now signal that there is no more data available: the onCompleted() event
  • The producer can now signal that an error occurred: the onError() event
  • RxJava Observables can be composed instead of nested, saving the developer from the callback hell

Observables and Iterables share a similar API; lots of operations that we can perform on Iterable can be performed on Observable too. Of course, due to the "flow" nature of Observables, we don't have equivalents for methods such as Iterable.remove().

Pattern

Single return value

Multiple return values

Synchronous

T getData()

Iterable<T> getData()

Asynchronous

Future<T> getData()

Observable<T> getData()

From a semantic point of view, RxJava is .NET Rx. From a syntactical point of view, Netflix takes care of porting every Rx method, keeping in mind Java code conventions and basic patterns.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.254.118