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?.
AsyncFunction
and Function
in transform(ListenableFuture<I> input, Function/AsyncFunction <? super I,? extends O> function)
? (AsyncFunction only used for nested Futures.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;
}
Well you didn't write the full method signature in your question
addCallback
returns nothingtransform
returns a future that holds result of the function (if the input succeeded) or the original input's failure (if not). This allows to chain transformations, with a fluent syntax.I've not used AsyncFunction
, but I understand they add one level of asynchronicity, ie the result of the Future
is another Future
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With