Asynchronous callbacks in Springboot (Part 1)

A CallBack Function is a function that is passed into another function as an argument and is expected to execute after some kind of event.The purpose of callback function is to inform the class that the work is done and if any operation needs to be performed can be performed.
Real scenario where asynchronous are used is

  • Delete all the data stored in various tables/index and make sure that no one waits for the other to finish.
  • Calling more than one REST API and expecting some kind of response.Each Request should be independent of each other and should not wait.

If I want to call some function asynchronously inside some class or some method outside the class,I have often used CompletableFuture class in java.
A CompletableFuture is used for asynchronous programming.It means writing non-blocking code.Each task is allocated a seperate thread and its status is returned when it gets completed.
Since each thread runs parallely ,the execution time reduces.

CompletableFuture extends Future interface(which was introduced in JAVA 5) and apply some additional completion(extends CompletionStage) logic.

It belongs to java.util.concurrent package.

Important Function are as follows:

public class Asynchronous {

    public static int mutiply(int num) {
        return 2 * num;
    }

    public static void main(String[] args) {
        Integer[] arr = {1, 2, 3, 4};
        for (int i : arr) {
            CompletableFuture
                    .runAsync(() -> {
                        System.out.println("The product with " + i + " is: " + mutiply(i));
                    });
        }
    }
}

runAsync(): Completes the job Asynchronously.but does not return anything

public class Asynchronous {

    public static int mutiply(int num) {
        return 2 * num;
    }

    public static void printingValues(int index, int num) {
        System.out.println("The product with " + index + " is: " + num);
    }

    public static void main(String[] args) {
        Integer[] arr = {1, 2, 3, 4};
        for (int i : arr) {
            CompletableFuture<Integer> submitTask = CompletableFuture
                    .supplyAsync(() -> {
                        return mutiply(i);
                    });
            //thenAcceptAsync takes the return value of the supplyAsync for that thread and executes the operation
            submitTask.thenAcceptAsync(value -> {
                printingValues(i, value);
            });
        }
    }
}

supplyAsync(): It also completes the job Asynchronously but it returns CompletableFuture on which we can apply other methods.

thenAcceptAsync():It takes that CompletableFuture for that thread and perform the operation on that CompletableFuture. It works as a callback function.

CompletableFuture<String> future1
                = CompletableFuture.supplyAsync(() -> "the");
        CompletableFuture<String> future2
                = CompletableFuture.supplyAsync(() -> "bad");
        CompletableFuture<String> future3
                = CompletableFuture.supplyAsync(() -> "engineer");

        CompletableFuture<Void> combinedFuture
                = CompletableFuture.allOf(future1, future2, future3);

allOf():Combines all the CompletableFuture and waits for each to get completed before performing any other operation.