Core Java

Frameworks and toolkits to make Java reactive: RxJava, Spring Reactor, Akka and Vert.x overview

Today people want highly responsive, interactive applications with strong user experience, which often means dealing with asynchronicity, especially when the apps are about high-load, real-time data and multi-userness.

As Java is an object-oriented language that inherently supports the imperative programming style, asynchronicity is quite a troublesome issue that can turn the code into complete hell. One of the possible ways to tackle complications with asynchronicity is to introduce ‘reactivity’ on the coding level (reactive programming) or on the level of design and architecture (reactive systems).

Let’s run through the most popular frameworks and toolkits to understand what options we have today for going reactive in Java and how they differ (if they do). However, it is a good idea to start with refreshing some basic terms from the ‘reactive’ context.

1. A quick brushup on ‘reactivity’

Reactive Streams specification appeared in 2015 as an initiative to provide a standard for asynchronous stream processing with non-blocking backpressure that could ensure interoperability of reactive libraries. The initiative does not introduce anything new but rather brings together a set of rules and agreements that simplify and standardize ’reactivity’ across multiple tools. It introduces four core interfaces (PublisherSubscriber, Subscription and Processor). A Publisher produces a sequence of events and directs it to Subscribers. A Subscriber receives data from the Publisher and produce side effects. Subscription is the connection between the Publisher and the Subscriber. A Processor comprises both Publisher and Subscriber protocols.

Non-blocking environment means that a thread is never held wasted awaiting for other operations to complete.

Backpressure is an ‘important feedback mechanism’ that allows managing cases when data is produced quicker than a receiver can consume it. It gives the data producer a possibility to keep to the right pace and not to fail under the high load.

Message-driven and event-driven approaches. With the message-driven approach, the producer knows exactly to whom he sends the message, while the event-driven approach means that a receiver just subscribes to the data producer, which keeps the list of all its subscribers and notifies them in case of any change in its state.

2. Java Reactive programming

There are two most commonly used frameworks for implementing reactive coding into a Java application – RxJava and Spring Reactor.

RxJava was the first full-fledged reactive tool for Java. In the distant 2009, Erik Meijer introduced this new programming style with Reactive Extensions that were implemented as a set of Microsoft libraries for creating asynchronous and event-driven programs by using by default observable sequences for .NET 4.0. Soon Rx.NET won love of developers and the world saw other implementations: RxJava (November 2014), RxSwift, RxScala, RxJS, Rx.Kotlin, etc. RxJava 2.0 was re-written from scratch atop the Reactive Streams specification with new types of data producers introduced.

For the first time, the world saw Spring Reactor in 2013. It greatly resembles RxJava and represents API implementation of Spring 5. Though having much in common, there are some differences between the two frameworks. Let’s quickly explore them with the chart below.

 RxJavaSpring Reactor

Fourth generation libraries

Latest release, as for 2018June 21, 2017 – RxJava 2.1.1.November 16, 2017 – Reactor Core 3.1.2.
EnvironmentReactiveXSpring 5
Java version Java 6 (both Java and Java for Android)Java 8

Single-threaded non-blocking by default

Event-driven

Support of reactive streamsSupports Reactive Streams partially*Supports Reactive Streams fully
Types of data producersRxJava 1 – Observable

RxJava 2 – Observable (multiple values), Flowable (multiple values, supports backpressure), Single (one value or error), Maybe (one value, error or no emissions), Completable (completes with error or success)

Controllers:

Flux  represents asynchronous sequences of 0-n values and Mono – those of 0-1values (both support backpressure)

Number of operatorsOver 100 operators**A set of core operators

* RxJava has only Flowables as a true embodiment of Publishers. The rest of data producers – namely, Single, Maybe, Observable, Completable – do not support backpressure.

** Both RxJava and Reactor allow applying to the immersive power of operators (map, zip, filter, etc.) to transform and modify the main stream. The operators are greatly illustrated by Marble diagrams (find some entertainment here). They do not belong to the initial reactive programming theory and look like a part of functional programming. The operators greatly simplify code writing as they represent a convenient way to transform data streams, while in ‘usual’ Java, a developer has to write patiently every single step that a computer will undertake. On the other hand, the operators allow fusing together considerable sets of steps with a certain function, which makes the code much clearer, leaner, and easier to understand. Besides, the operators are compatible with each other.

3. Reactive systems in Java

Reactive systems represent the next level of ‘reactivity’. Akka and Vert.x are the frameworks usually used to build these systems that are most often implemented as reactive microservices. This new term in the IT world literally means the combination of microservices architecture and reactive system principles. The gains of ‘reactivity’ work perfectly for these distributed systems making them even more flexible and reliable as well as increasing their performance.

Lightbend introduced Akka in July 2009 (the latest release: January 11, 2018 – Akka 2.5.9). The toolkit was originally written in Scala, but today it can be also used in Java. Akka is open-source and supports the Reactive Streams specification. The toolkit’s idea was to introduce the high availability and scalability of the Erlang platform to JVM.

The actor model is a base for Akka. Actors are independent sets of code that communicate with each other through messages. An actor’s mailbox receives messages and enqueues them with a single thread of control.

Moreover, Akka stands out with the actor tree hierarchy – a special form of actor organization that implies the existence of the parent-child relation between actors of different levels and thus ensures particular failure tolerance and self-recovery. A parent-actor takes care of a child block: when the parent-actor receives a notification about a child’s crash, it can resume, restart, stop its child or stop itself to transfer responsibility for error handling to the next level. As actors represent completely isolated units and don’t share any mutable states, such sudden stops of one of them won’t affect other actors and they will be able to continue work as usual.

The first release of Vert.x was in 2012 (the latest release: February 13, 2018 – Vert.x 3.5.1). Vert.x is an open-source toolkit and has Node.js as its force behind. Vert.x supports Reactive Streams and offers verticles instead of actors. Unlike Akka, Vert.x is a completely event-driven environment and communicates in a simple request-response fashion. Messages are sent to EventBus to be enqueued with a single thread of control. Vert.x is a polyglot and easily cooperates with any JVM or non-JVM language.

Both Akka (with the help of ActorRef) and Vert.x (with the help of Service Proxies) simplify remote calls for distributed systems. As their components communicate via ActorRef/Service Proxies that resemble URL, there is no reason to care whether its’s a local call or not.

Takeaways:

  • Akka is a set of mature libraries with good knowledge base that implies less time and effort for its implementation.
  • x is younger than Akka, so it’s not that popular and not so well-described.
  • Vert,x is a polyglot and thus more convenient when it comes to combining different languages in the process of development.
  • Akka actors form a tree hierarchy that allows graceful management of high load and failures.
  • Akka is message-driven, which makes its components (actors) even more loosely coupled.

4. On a final note

In this article, we’ve given a quick review of ‘reactive’ frameworks and toolkits used in the Java world. They steadily gain traction and demonstrate that ‘reactivity’ is not just an overhyped concept but quite a viable approach for Java application development on the level of both coding and architecture.

Java developers may choose among various players in the reactive context that are quite similar to each other though also preserve some distinguishing features. The good news is that they all support the Reactive Streams standard that makes them interoperable. Therefore, developers can either choose what suits their specific needs or try the best of each for their application.

ScienceSoft

ScienceSoft is an international software development and IT consulting company with a vast 29+ year experience in delivering IT solutions based on Java technologies and frameworks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
kathleen
6 years ago

Nice blog on frameworks java is very well recognized for it’s uniqueness and you have explained it very well.

NIX United
4 years ago

Nice post. By the way, Spring Framework in 2020 is still in the leading positions.

jennyaa
jennyaa
4 years ago

Nice Information about frameworks.

Steve Brown
3 years ago

Really Informative article about Framework and Java React Native tool.

Raveena
3 years ago

Thank you for the article, Am not a coder still I read the whole article such a wonderful article that I could also understand something from this.

Back to top button