Class AsyncCompletionScope

java.lang.Object
io.fluxzero.sdk.common.AsyncCompletionScope

public final class AsyncCompletionScope extends Object
Thread-local scope for asynchronous work started from completion callbacks.

Completion callbacks sometimes need to start asynchronous side effects, such as aggregate commits, without blocking each callback immediately. This scope lets those callbacks register their futures and waits for all registered work after the callback group has finished running.

The scope is intentionally thread-local. Work registered from other threads is only included when that thread is executing inside the same logical scope.

  • Method Details

    • runAndAwait

      public static void runAndAwait(Runnable task)
      Runs the supplied task inside a new completion scope and waits for all futures registered in that scope.

      If both the task and asynchronous completion fail, the asynchronous failure is added as a suppressed exception to the task failure.

      Parameters:
      task - the task that may register asynchronous completion work
    • register

      public static <T> CompletableFuture<T> register(CompletableFuture<T> future)
      Registers a future with the current completion scope.

      When no scope is active, the future is returned unchanged and no waiting behavior is added. This makes callers free to register futures unconditionally while preserving the normal behavior outside handler or batch completion.

      Type Parameters:
      T - result type of the future
      Parameters:
      future - future to await at the end of the active scope; null is treated as already completed
      Returns:
      the supplied future, or a completed future when future is null
    • register

      public static <T> CompletableFuture<T> register(CompletableFuture<T> future, Runnable afterCompletion)
      Registers a future with the current completion scope and runs a callback after scoped futures have completed.

      The callback runs on the thread that owns the completion scope, after all registered futures have completed and before any asynchronous failure is rethrown from runAndAwait(Runnable). This is useful for cleaning up thread-local state that must remain visible until asynchronous completion work has finished.

      When no scope is active, the future is returned unchanged and the callback is not invoked.

      Type Parameters:
      T - result type of the future
      Parameters:
      future - future to await at the end of the active scope; null is treated as already completed
      afterCompletion - callback to run after scoped futures have completed; may be null
      Returns:
      the supplied future, or a completed future when future is null
    • isActive

      public static boolean isActive()
      Returns whether the current thread is executing inside an async completion scope.
      Returns:
      true when futures registered by this thread will be awaited by a surrounding scope