Posts

Showing posts with the label java8

Java8 CompletableFuture API for Asynchronous Programming in springboot microservices

Introduction Lambda expressions are by far the most discussed and emphasized feature of Java 8. While I agree that Lambdas being the salient feature in Java8, still there are lot more new things hidden behind the shadows of lambda expressions. Today we gonna take a look at one of them, the CompletionStage API [1] for leveraging asynchronous programming. Java5 introduced the ExecutorService pattern where programmers can submit tasks to a pool of Threads. This yields a Future object and the only way to get the result from a future object is to call its get() method in the thread that submitted the task. This method is blocking, so this call will block the thread until the result is available to process. So, the use of  Future is ruled out on performance grounds. This is exactly where the CompletionStage comes to the rescue. [2] What Is a CompletionStage In a nutshell, a CompletionStage is a model that carries a task. A task can be an instance of Runnable, Consumer, or Functio...

Leveraging Multicore CPU architecture with fork-join framework in Java

Image
Introduction Hardware trends drive programming idioms. The Java language has had support for threads and concurrency from day 1; the language includes synchronization primitives such as synchronized and volatile, and the class library includes classes such as Thread. However, the concurrency primitives that were sensible in 1995 reflected the hardware reality of the time: most commercially available systems provided no parallelism at all, and even the most expensive systems provided only limited parallelism. In those days, threads were used primarily for expressing asynchrony, not concurrency, and as a result, these mechanisms were generally adequate to the demands of the time. Going forward, the hardware trend is clear; Moore's Law will not be delivering higher clock rates, but instead delivering more cores per chip. As we enter the multi-core era, we will need to find finer-grained parallelism or risk keeping processors idle even though there is plenty of work to do. As the d...

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 ...

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

Image
Introduction Well, today we are going to discuss an advanced RxJava operator called Zip. Let’s first take a look at the zip operator at a glance and then move on to a real world example that leverages the capabilities of the operator. In fact, zip is a very powerful and useful operator provided in the RxJava library. Prerequisites : Good knowledge of Java8, Functional Programming, RxJava, Spring Boot, Microservices. User Level : Advanced Since this is an advanced article, I am not going to walk you through the basics of RxJava or Spring Boot microservices. If you need to get some basic understanding of RxJava, I thoroughly recommend you to go through one of my previous blog posts [1] [2] or any other good introductory article about RxJava. If you need to learn more about Spring Boot microservices please go through some of the spring tutorials which are really comprehensive and impressive. RxJava Zip Operator Combines the emissions of multiple Observables together via a speci...

Introducing Java Reactive Extentions in to a SpringBoot Micro Service

Image
Introduction Today we are going to take a look at Reactive Programming using Java8 and RxJava, which is a library for composing asynchronous and event-based programs by using observable sequences. RxJava stands for Reactive Extensions for the JVM. It extends the observer pattern [Gamma95] to support sequences of data/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 and concurrent data structures [1]. Pre-Requisites : Having a good knowledge about Java8, Functional Programming and spring boot microservices. What is Reactive programming? In essence, Reactive programming is an asynchronous programming paradigm concerned with data streams. The system time, a list of entries from the DB/API, user clicks, everything can be a stream of data. Data from different streams can easily be combined and transformed, and in the end processed/observed by the ...