Introducing Java Reactive Extentions in to a SpringBoot Micro Service

Today I am going to teach you about RxJava, which is a new programming paradigm to do asynchronous programming easily. RxJava stands for Reactive Extensions for Java. Writing a blocking code is evil and deteriorates the performance of your application. So we should focus on writing asynchronous code which never keeps you blocking. There are two approaches to writing asynchronous code, one is using callbacks the other is using RxJava. Callbacks has low composition and lead to callback hell if you have many callbacks back to back. The code becomes more and more complex. That is where RxJava comes to our rescue. RxJava was a project that has been open sourced by the Netflix.


In RxJava Iterators and Iterables become Observers and Observables. Iterable and Iterator is a pull model. In RxJava Observable and Observer is a push model. Push model is preferred over the pull model. You will subscribe to a stream and you will receive notifications. The source will notify you when it emits new items. RxJava helps us to compose asynchronous programs elegantly.

Pre-Requisites: Having some knowledge about spring boot and microservices. Also you need to have Java 8 installed in your system.

Now I am going to teach you how to use RxJava components to write a fully asynchronous spring boot micro service. You may find the sample project in GitHub at [1]. All the necessary steps to run the project was given in the readme file, hence I am not going to discuss it again here.

This application implements a Spring Boot microservice using RxJava. All the external interactions are asynchronously done using RxJava components and programmatic devices. This explains you how to bridge the gap between RxJava and Spring Boot while integrating them together. What this application does is when you send a GET request like this [3], it gives you the currency rates for the given currencies compared to a base currency which is EURO in this case. The sample response payload is given below.

The application merely calls external currency conversion API [2] to get the work done. This is a fully asynchronous implementation based on RxJava. The high level component diagram of the system being considered is depicted below.

Let’s first check out the code [1]. The entry point to any spring boot application is the Controller or Resource class which defines some basic presumably CRUD operations related to a resource in REST and associate them with classic HTTP verbs in the rest world. Here our resource class is CurrencyResource. This class has an instance of the service layer which merely implements business logic of our application. The service class named CurrencyConverter uses RxJava components to call an external currency converter REST API [2] asynchronously. Inside the getCurrencyRatesObservable method it creates the Observable. This is your source of data. The sample code is given below.

Here there’s a creation of an Observable from the scratch. It tells when you get a new subscription, emit onNext on this subscription and signal completion.

At the end you subscribe an Observer to your Observable stream. There you describe what you want to do with the final form of the data. The Observer is a simple interface with three methods.

onNext() - method that gets notified when there is a new item emitted in the stream.
onComplete() - When the source has no elements to emit, it will notify the consumer to complete
onError() - Notifies about exceptions and exceptions are propagated up.

We will receive end calls to onNext, followed either by onCompleted or onError.

The subscription to the stream is made inside the Resource method and that code is given below.

There are tons of operators available in RxJava in different categories such as creation, filtering, counting, transforming, combining, retrying and many more to assist developers. It is not possible to cover all of those operators in a simple blog post like this. Therefore I thoroughly reckon you to try those operators. They are very robust operators which lets you write simple, elegant, composable, scalable and efficient code. That does it for another blog post related to RxJava. You can get my code [1], and try to play with it while implementing different RxJava operators by your own. I’ll see you in my next blog post. Happy coding !

[3] http://localhost:8080/api/currencyconverter/rates?symbol=USD,GBP


Popular posts from this blog

SSL Debugging in WSO2 ESB

Combine the emissions of multiple Observables together using RxJava Zip operator in a Spring Boot Micro service