What is the difference between Futures.addCallBack()
and Futures.transform()
in Google Guava Concurrency.
As per the documentation:
Futures.addCallBack():addCallback(ListenableFuture<V> future, FutureCallback<? super V> callback)
Registers separate success and failure callbacks to be run when the Future's computation is complete or, if the computation is already complete, immediately.
Futures.transform():transform(ListenableFuture<I> input, AsyncFunction<? super I,? extends O> function)
Returns a new ListenableFuture whose result is asynchronously derived from the result of the given Future.
As per my understanding addCallback()
will register success or failure callback when asynchronous processing is completed. In this case we can handle the out put based on success or failure conditions (example: logging, flow control..etc). and transform()
only return the Asynchronous object back. So difference is only Callback?.
- whether my understanding is correct?
- Which is the best one to use with asynchronous processing?
- If I need to call multiple asynchronous methods in a sequence, is there any best practice for that?
- What is the difference between using
AsyncFunction
andFunction
intransform(ListenableFuture<I> input, Function/AsyncFunction <? super I,? extends O> function)
? (AsyncFunction only used for nestedFutures.transform()
?)
What I tried:
I try to write code like below, whether this is a good practice or not.
public ListenableFuture<MyObject> doSomething() {
logger.info( "Entered in dosomething() Method." );
ListeningExecutorService executor =
MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(50));
ListenableFuture<MyObject> myAsyncObject =
calculator.calculateSomething(input);
//internally calculator.calculateSomething() have multiple asynchronous
// calls and I am using Futures.transform(), without callback.
Futures.addCallback(myAsyncObject, new FutureCallback<MyObject>() {
public void onSuccess(MyObject result) {
logger.info( "Calculation Completed successfully." );
//TODO: check for success and log it.
}
public void onFailure(Throwable thrown) {
logErrorDetails(thrown);
}
}, executor);
executor.shutdown();
return myAsyncObject;
}