Class HookAdapters


  • public final class HookAdapters
    extends java.lang.Object
    Utility class for adapting synchronous hooks to asynchronous hooks.

    This class provides adapter methods that convert synchronous hook implementations (Hook.BeforeRequest, Hook.AfterSuccess, Hook.AfterError) to their asynchronous counterparts (AsyncHook.BeforeRequest, AsyncHook.AfterSuccess, AsyncHook.AfterError).

    Performance Note: The execution of synchronous hooks is offloaded to the global ForkJoinPool. For better performance in high-throughput scenarios, consider re-implementing hooks using non-blocking I/O (NIO) patterns instead of relying on these adapters.

    Thread Safety: All adapter methods are thread-safe and can be called concurrently from multiple threads.

    See Also:
    Hook, AsyncHook, ForkJoinPool.commonPool()
    • Method Detail

      • toAsync

        public static AsyncHook.BeforeRequest toAsync​(Hook.BeforeRequest beforeRequestHook)
        Adapts a synchronous Hook.BeforeRequest to an asynchronous AsyncHook.BeforeRequest.

        The synchronous hook execution is offloaded to the global ForkJoinPool. Any exceptions thrown by the synchronous hook are wrapped as unchecked exceptions and propagated through the returned CompletableFuture.

        Performance Consideration: For high-throughput applications, consider implementing the hook directly using NIO patterns rather than using this adapter, as it avoids thread pool overhead and blocking operations.

        Parameters:
        beforeRequestHook - the synchronous before-request hook to adapt
        Returns:
        an asynchronous before-request hook that executes the synchronous hook in the global ForkJoinPool
        Throws:
        java.lang.NullPointerException - if beforeRequestHook is null
      • toAsync

        public static AsyncHook.AfterError toAsync​(Hook.AfterError afterErrorHook)
        Adapts a synchronous Hook.AfterError to an asynchronous AsyncHook.AfterError.

        This method handles the conversion between different response body types:

        • Converts HttpResponse<Blob> to HttpResponse<InputStream> for the synchronous hook
        • Converts the result back to HttpResponse<Blob> for the asynchronous interface

        The synchronous hook execution is offloaded to the global ForkJoinPool. Any exceptions thrown by the synchronous hook are wrapped as unchecked exceptions and propagated through the returned CompletableFuture.

        Performance Consideration: For high-throughput applications, consider implementing the hook directly using NIO patterns rather than using this adapter, as it avoids thread pool overhead and blocking I/O operations.

        Parameters:
        afterErrorHook - the synchronous after-error hook to adapt
        Returns:
        an asynchronous after-error hook that executes the synchronous hook in the global ForkJoinPool
        Throws:
        java.lang.NullPointerException - if afterErrorHook is null
      • toAsync

        public static AsyncHook.AfterSuccess toAsync​(Hook.AfterSuccess afterSuccessHook)
        Adapts a synchronous Hook.AfterSuccess to an asynchronous AsyncHook.AfterSuccess.

        This method handles the conversion between different response body types:

        • Converts HttpResponse<Blob> to HttpResponse<InputStream> for the synchronous hook
        • Converts the result back to HttpResponse<Blob> for the asynchronous interface

        The synchronous hook execution is offloaded to the global ForkJoinPool. Any exceptions thrown by the synchronous hook are wrapped as unchecked exceptions and propagated through the returned CompletableFuture.

        Performance Consideration: For high-throughput applications, consider implementing the hook directly using NIO patterns rather than using this adapter, as it avoids thread pool overhead and blocking I/O operations.

        Parameters:
        afterSuccessHook - the synchronous after-success hook to adapt
        Returns:
        an asynchronous after-success hook that executes the synchronous hook in the global ForkJoinPool
        Throws:
        java.lang.NullPointerException - if afterSuccessHook is null