Using a ReturnValueHandler to adapt RxJava Observables to Deferredresult in a SpringBoot micro service

If you look at my previous blog posts related to RxJava and Spring Boot microservices you may notice that all the subscriptions to the RxJava Observables are made in each and every controller method and controller method is responsible for the transformation between RxJava Observables and Spring aware DeferredResults. This is obvious one to one mapping that you can do between RxJava and Spring. In fact you can go further and generalize this behaviour. This will eliminate lots of boilerplate code in your application and make your code much more simple and readable. Also this brings a good software engineering practice called generalization and reuse into our code. Take a look at the following controller code before adding the return value handler.
We can use a simple ReturnValueHandler that calls an adapter[Gamma95] that will adapt Observables to DeferredResults. By registering this value handler with Spring, all the methods that return an Observable will be reworked into returning a DeferredResult. When you use DeferredResults as a return type you are instructing Spring that your REST service is asynchronous. By doing this you get the benefits of asynchronous programming, having more efficient usage of your resources. Our first step is to write code for the return value handler which is given below.
Incidentally, note the use of the static nested class to avoid any memory leaks. Next this value handler should be registered with Spring. For that we need to have following configuration class in place.
Then we can get back to our Controller or Resource classes and dispense with all the boilerplate code used to subscribe to the Observables and adapt them to DeferredResults. Now our controller classes merely return RxJava Observables. Here’s how it looks in practice. For brevity’s sake, import statements are omitted.
Notice how simple and readable this code is compared to the previous counterpart of our Resource class. You may find the source code of the project here [1].

Conclusion

Well, today we kept a step beyond the previous incarnation of our RxJava spring boot microservice by enhancing it to use a return value handler to adapt RxJava Observables into spring aware DeferedResults. We generalized asynchronous response handling code while making it more simple and readable. When it comes to code, clarity and simplicity are of paramount importance.

References

[Gamma95] https://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional-ebook/dp/B000SEIBB8
[1] https://github.com/ravindraranwala/SpringBootRxJava

Comments

Popular posts from this blog

Introducing Java Reactive Extentions in to a SpringBoot Micro Service

Optimal binary search trees

Edit distance