Posts

Showing posts from November, 2018

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