Interface AsyncCatchFunction<R,E extends Throwable>

Type Parameters:
R - the type of the result of the asynchronous operation
E - 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.

@FunctionalInterface public interface AsyncCatchFunction<R,E extends Throwable> extends CatchFunction<R,E>
The AsyncCatchFunction interface represents a function that handles exceptions occurring within an asynchronous operation. It extends the CatchFunction interface and adds the capability to perform asynchronous exception handling.

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:
  • Method Details

    • applyAsync

      void applyAsync(R r, E e) throws IOException
      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 null
      e - the caught exception
      Throws:
      IOException - if an I/O error occurs during the application of the function
    • apply

      default R apply(R r, E e) throws IOException
      Synchronously applies this function to the given result and exception.

      This method first calls applyAsync to initiate the asynchronous handling of the exception. Then, it waits for the asynchronous operation to complete by calling result, which retrieves the result of the current thread's CompletableFuture.

      Specified by:
      apply in interface CatchFunction<R,E extends Throwable>
      Parameters:
      r - the result of the asynchronous operation, if any; may be null
      e - 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

      default CompletableFuture<R> async(R r, E e) throws IOException
      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 null
      e - 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

      default CompletableFuture<R> apply(CompletableFuture<R> in, Class<E> eClazz)
      Applies the catch function to a CompletableFuture, 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 CompletableFuture and 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:
      apply in interface CatchFunction<R,E extends Throwable>
      Parameters:
      in - the input CompletableFuture to which the catch function is applied
      eClazz - the class object representing the exception type to catch
      Returns:
      a new CompletableFuture that completes with the result of applying the catch function, or propagates the original exception if it does not match the specified type