Interface FinallyFunction<R>
- Type Parameters:
R- the type of the result of the asynchronous operation
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
The
FinallyFunction interface represents a function that is used to perform
final actions after an asynchronous operation completes, regardless of whether the
operation was successful or resulted in an exception. This interface is part of
the asynchronous utilities provided by the Hadoop Distributed File System (HDFS)
Federation router.
A FinallyFunction is typically used for cleanup or finalization tasks,
such as releasing resources, closing connections, or performing other actions that
need to occur after all other operations have completed.
An implementation of this interface should define what the final action is. It takes the result of the asynchronous operation as an argument and returns a new result, which can be the same as the input result or a modified version of it.
FinallyFunction is used to implement the following semantics:
try{
R res = doAsync(input);
} catch(...) {
...
} finally {
// Can use FinallyFunction
R result = thenApply(res);
}
-
Method Summary
Modifier and TypeMethodDescriptiondefault CompletableFuture<R>apply(CompletableFuture<R> in) Applies this final action function to aCompletableFuture, which is expected to be the result of an asynchronous operation.Applies this final action function to the result of an asynchronous operation.
-
Method Details
-
apply
Applies this final action function to the result of an asynchronous operation.- Parameters:
r- the result of the asynchronous operation, which may be null if the operation did not complete successfully- Returns:
- the result after applying the final action, which may be a new result or a modified version of the input result
- Throws:
IOException- if an I/O error occurs during the application of the final action
-
apply
Applies this final action function to aCompletableFuture, which is expected to be the result of an asynchronous operation.This method is a convenience that simplifies the use of
FinallyFunctionwith asynchronous operations. It handles the completion of the future and applies theFinallyFunctionto the result.- Parameters:
in- theCompletableFuturerepresenting the asynchronous operation- Returns:
- a new
CompletableFuturethat completes with the result of applying the final action function
-