I am going to talk about the Async in this Section. Actually, this posting will be based on other posting we will continue. Recently, for Async, Reactive and Lambda are so important. Before we continue this posting,
I assume that you have some knowledge about Future, Executor in Java.
If you want to use Async, we should use Thread such as Thread, Callable, Future etc. Particularly, Future is important. But, when Future revealed or we learn Future at first, it gots many people to confuse why there is get() because get() has a role as blocking until returning a result. But it is useful to use to where we want to make asynchronous things.
At first, I am going to tell about FutureTask. I won’t mention about how to use FutureTask because there are many web site which explain about that. FutureTask provides us with the ways for giving us the data of the result without not using get().
Look at below.
code-1
If you have an error about lombok, you can use System.out.println() instead of log.info(). I will talk about Lombok later. In this code, The important lines are from 14 to 19 and from 21 to 31. The first thing which is from 14 to 19 is about what you want to do like a business logic. This is a constructor parameter which can consist of Lambda or method. And we also can override the method which has a role that can conduct somethings after everything is done without get() method: Line(:from 21 to 31)
What is the result?
Actually, FutureTask is not a callback method, and it is difficult to handle errors. If you want to handle errors, you should make a code using a code block like try and if. But we can make extended class about FutureTask which has a callback method that makes us handle an error easily.
code-2
We have same result above. If you want to check about error callback, you run a code with line:44. In Java, there is an async class which is AsynchronousByteChannel made in 1.7jdk. AsynchronousByteChannel has a method which is read().
void read(ByteBuffer dst, A attachment, CompletionHandler<Integer,? super A> handler);
We need to pay attention to CompletionHandler.
public interface CompletionHandler<V, A> {
/**
* Invoked when an operation has completed.
*
* @param result
* The result of the I/O operation.
* @param attachment
* The object attached to the I/O operation when it was initiated.
*/
void completed(V result, A attachment);
/**
* Invoked when an operation fails.
*
* @param exc
* The exception to indicate why the I/O operation failed
* @param attachment
* The object attached to the I/O operation when it was initiated.
*/
void failed(Throwable exc, A attachment);
}
CompletionHandler interface has two methods which is similar to what we make code above. They have a similar role. completed() method is similar to onSuccess we wrote above, failed() method is similr to onError we wrote aboce. If it was made in 1.8jdk through seperated interface, we would use it with Lambda.
Code-2 has the codes of two type. One is business logic, and the other is technical code. So, we need to package the technical code commonly. So, I am going to talk about how to abstract the technical commonly and the ways to use async in Spring.