Interface AsyncCatchFunction<R,E extends Throwable>
- Type Parameters:
R- the type of the result of the asynchronous operationE- the type of the exception to catch, extending Throwable
- All Superinterfaces:
Async<R>,CatchFunction<R,E>
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
This interface is part of the asynchronous utilities provided by the Hadoop Distributed File System (HDFS) Federation router. It is used in conjunction with other asynchronous interfaces such as AsyncRun to build complex, non-blocking operations.
An implementation of this interface should define how to handle a caught exception asynchronously. It takes two parameters: the result of the asynchronous operation (if any) and the caught exception. The function can then initiate an asynchronous process to handle the exception, which may involve logging the error, performing a recovery operation, or any other custom logic.
For example, the applyAsync method is intended to be called when an exception is caught during an asynchronous operation. It should initiate the asynchronous process to handle the exception without blocking the main execution thread.
The default method apply is provided to allow synchronous operation in case the asynchronous handling is not required. It simply calls applyAsync and waits for the result.
The default method async is provided to allow chaining with other asynchronous operations. It calls applyAsync and returns a CompletableFuture that completes with the result of the operation.
AsyncCatchFunction is used to implement the following semantics:
try{
R res = doAsync1(input);
} catch(E e) {
// Can use AsyncCatchFunction
R result = doAsync2(res, e);
}
- See Also:
-
Field Summary
Fields inherited from interface org.apache.hadoop.hdfs.server.federation.router.async.utils.Async
CUR_COMPLETABLE_FUTURE -
Method Summary
Modifier and TypeMethodDescriptiondefault CompletableFuture<R>apply(CompletableFuture<R> in, Class<E> eClazz) Applies the catch function to aCompletableFuture, handling exceptions of a specified type.default RSynchronously applies this function to the given result and exception.voidapplyAsync(R r, E e) Asynchronously applies this function to the given exception.default CompletableFuture<R>Initiates the asynchronous application of this function to the given result and exception.Methods inherited from interface org.apache.hadoop.hdfs.server.federation.router.async.utils.Async
getCurCompletableFuture, result, setCurCompletableFuture
-
Method Details
-
applyAsync
Asynchronously applies this function to the given exception.This method is intended to be called when an exception is caught during an asynchronous operation. It should initiate the asynchronous process to handle the exception without blocking the main execution thread. The actual handling of the exception, such as logging the error, performing a recovery operation, or any other custom logic, should be implemented in this method.
- Parameters:
r- the result of the asynchronous operation, if any; may be nulle- the caught exception- Throws:
IOException- if an I/O error occurs during the application of the function
-
apply
Synchronously applies this function to the given result and exception.This method first calls
applyAsyncto initiate the asynchronous handling of the exception. Then, it waits for the asynchronous operation to complete by callingresult, which retrieves the result of the current thread'sCompletableFuture.- Specified by:
applyin interfaceCatchFunction<R,E extends Throwable> - Parameters:
r- the result of the asynchronous operation, if any; may be nulle- the caught exception- Returns:
- the result after applying the function
- Throws:
IOException- if an I/O error occurs during the application of the function
-
async
Initiates the asynchronous application of this function to the given result and exception.This method calls applyAsync to start the asynchronous operation and then retrieves the current thread's CompletableFuture using getCurCompletableFuture. It returns this CompletableFuture, which will be completed with the result of the asynchronous operation once it is finished.
This method is useful for chaining with other asynchronous operations, as it allows the current operation to be part of a larger asynchronous workflow.
- Parameters:
r- the result of the asynchronous operation, if any; may be nulle- the caught exception- Returns:
- a CompletableFuture that will be completed with the result of the asynchronous operation
- Throws:
IOException- if an I/O error occurs during the initiation of the asynchronous operation
-
apply
Applies the catch function to aCompletableFuture, handling exceptions of a specified type.This method is a default implementation that provides a way to integrate exception handling into a chain of asynchronous operations. It takes a
CompletableFutureand a class object representing the exception type to catch. The method then completes the future with the result of applying this catch function to the input future and the specified exception type.If the input future completes exceptionally with an instance of the specified exception type, the catch function is applied to the exception. Otherwise, if the future completes with a different type of exception or normally, the original result or exception is propagated.
- Specified by:
applyin interfaceCatchFunction<R,E extends Throwable> - Parameters:
in- the inputCompletableFutureto which the catch function is appliedeClazz- the class object representing the exception type to catch- Returns:
- a new
CompletableFuturethat completes with the result of applying the catch function, or propagates the original exception if it does not match the specified type
-