In the last article, I shared my opinion which is about an async in the Spring Framework and I mentioned the one way that is to use the async through @Async among the two ways. So, I am going to share my opinion about the asycn using Callback Function. I have mentioned about how to use async using Callback Function in Java in the previous article (It was in Spring, not Java). Then, is it possible to use the FutureTaks in Spring? At least, my answer is ‘No’. That’s why async is controlled by Spring. To use the Callback Function in Spring, we can use ListenableFuture which is provided by Spring.(Actually, though there are more Future class provided by Spring, I will share it later.)

This is the code for ListenableFuture. In the last article, we used the the Future class and we should wait for Async thread that is finished through Future.get() in main thread. The difference between them is to use Callback (line:31-39). what is the good point if we use the ListenableFuture?

First at all, in my opinion, it can be to separate the thread into Servlet Thread and Async Thread. When we just used the Future without callback, we must wait for them which is Async thread. If there are a lot of request from client, the response of server should be delayed from waiting for Async thread.(Servlet Thread has a role which handles the request from client). So using callbakc could help an application to be a higher performance. Moreover, it is more useful to deal with the error. In the future class, we should use the try and catch grammar, yet we don’t have to use the try and catch. We just can use the callback function dealing with error and success.(line:31-39). Using callback is more clear than Future.get().

Through callback, in conclusion, we can make higher performance application. Especially, as service become bigger than before, high performance is one of the important elements. So, in the java camp, they also made a Future class for the high performance which named CompletableFuture. Many people have said like that Java is deficient in competition than other languages such as javascript(Nodejs) which has a mechanism about single thread. But, it is not true. Java is also following the modern trends well and trying to make a higher performance. Like this, we can make higher performance than ListenableFuture with combined various class including CompletableFuture. Then, How can we make high-performance application?