Interface Callback

All Superinterfaces:
Invocable
All Known Subinterfaces:
Blocker.Callback, Callback.Completing, HttpStream
All Known Implementing Classes:
AsyncMiddleManServlet.ProxyReader, AsyncMiddleManServlet.ProxyReader, AsyncMiddleManServlet.ProxyReader, AsyncMiddleManServlet.ProxyResponseListener, AsyncMiddleManServlet.ProxyResponseListener, AsyncMiddleManServlet.ProxyResponseListener, AsyncMiddleManServlet.ProxyWriter, AsyncMiddleManServlet.ProxyWriter, AsyncMiddleManServlet.ProxyWriter, AsyncProxyServlet.StreamReader, AsyncProxyServlet.StreamReader, AsyncProxyServlet.StreamReader, Callback.Completable, Callback.Nested, ControlFlusher, CountingCallback, DemandingFlusher, FutureCallback, GzipResponseAndCallback, HTTP2Session.Entry, HTTP2Stream, HttpDestination, HttpInput.Content, HttpInput.Content, HttpInput.EofContent, HttpInput.EofContent, HttpInput.ErrorContent, HttpInput.ErrorContent, HttpInput.SpecialContent, HttpInput.SpecialContent, HttpInput.WrappingContent, HttpInput.WrappingContent, HttpStream.Wrapper, InstructionFlusher, IteratingCallback, IteratingNestedCallback, MessageFlusher, ProxyHandler.ProxyResponseListener, ProxyProtocolClientConnectionFactory.ProxyProtocolConnection, SendHandlerCallback, SendHandlerCallback, SendHandlerCallback, SharedBlockingCallback.Blocker

public interface Callback extends Invocable

A callback abstraction that handles completed/failed events of asynchronous operations.

Semantically this is equivalent to an optimise Promise<Void>, but callback is a more meaningful name than EmptyPromise

  • Field Details

    • NOOP

      static final Callback NOOP
      Instance of Adapter that can be used when the callback methods need an empty implementation without incurring in the cost of allocating a new Adapter object.
  • Method Details

    • completeWith

      default void completeWith(CompletableFuture<?> completable)

      Completes this callback with the given CompletableFuture.

      When the CompletableFuture completes normally, this callback is succeeded; when the CompletableFuture completes exceptionally, this callback is failed.

      Parameters:
      completable - the CompletableFuture that completes this callback
    • succeeded

      default void succeeded()

      Callback invoked when the operation completes.

      See Also:
    • failed

      default void failed(Throwable x)

      Callback invoked when the operation fails.

      Parameters:
      x - the reason for the operation failure
    • from

      static Callback from(CompletableFuture<?> completable)

      Creates a non-blocking callback from the given incomplete CompletableFuture.

      When the callback completes, either succeeding or failing, the CompletableFuture is also completed, respectively via CompletableFuture.complete(Object) or CompletableFuture.completeExceptionally(Throwable).

      Parameters:
      completable - the CompletableFuture to convert into a callback
      Returns:
      a callback that when completed, completes the given CompletableFuture
    • from

      static Callback from(CompletableFuture<?> completable, Invocable.InvocationType invocation)

      Creates a callback from the given incomplete CompletableFuture, with the given blocking characteristic.

      Parameters:
      completable - the CompletableFuture to convert into a callback
      invocation - whether the callback is blocking
      Returns:
      a callback that when completed, completes the given CompletableFuture
    • from

      static Callback from(Runnable success, Consumer<Throwable> failure)
      Creates a callback from the given success and failure lambdas.
      Parameters:
      success - Called when the callback succeeds
      failure - Called when the callback fails
      Returns:
      a new Callback
    • from

      static Callback from(Invocable.InvocationType invocationType, Runnable success, Consumer<Throwable> failure)
      Creates a callback with the given InvocationType from the given success and failure lambdas.
      Parameters:
      invocationType - the Callback invocation type
      success - Called when the callback succeeds
      failure - Called when the callback fails
      Returns:
      a new Callback
    • from

      static Callback from(Runnable completed)
      Creates a callback that runs completed when it succeeds or fails
      Parameters:
      completed - The completion to run on success or failure
      Returns:
      a new callback
    • from

      static Callback from(Invocable.InvocationType invocationType, Runnable completed)

      Creates a Callback with the given invocationType, that runs the given Runnable when it succeeds or fails.

      Parameters:
      invocationType - the invocation type of the returned Callback
      completed - the Runnable to run when the callback either succeeds or fails
      Returns:
      a new Callback with the given invocation type
    • from

      static Callback from(Callback callback, Runnable completed)

      Creates a callback that runs the Runnable argument after completing the callback argument.

      The Runnable is assumed to be NON_BLOCKING, so the Invocable.InvocationType of the returned callback is the same as the callback argument.

      Parameters:
      callback - The nested callback
      completed - The Runnable to run after the nested callback is completed
      Returns:
      a new callback
    • from

      static Callback from(Callback callback, Consumer<Throwable> completed)

      Creates a callback that runs the Consumer argument after completing the callback argument.

      The Consumer receives null if the callback succeeded, or the Throwable failure if the callback failed.

      The Consumer is assumed to be NON_BLOCKING, so the Invocable.InvocationType of the returned callback is the same as the callback parameter.

      Parameters:
      callback - The nested callback
      completed - The Consumer to run after the nested callback is completed
      Returns:
      a new callback
    • from

      static Callback from(Runnable completed, Callback callback)
      Creates a callback that runs the Runnable argument before completing the callback argument.

      The Runnable is assumed to be NON_BLOCKING, so the Invocable.InvocationType of the returned callback is the same as the callback argument.

      Parameters:
      completed - The Runnable to run before the nested callback is completed. Any exceptions thrown from the Runnable will result in the callback failure.
      callback - The nested callback
      Returns:
      a new callback
    • from

      static Callback from(Callback callback, Throwable failure)

      Creates a callback which always fails the callback argument on completion.

      Parameters:
      callback - The nested callback
      failure - The cause to fail the nested callback; if the new callback is failed, the cause will be added to the failure argument as a suppressed exception.
      Returns:
      a new callback.
    • from

      static Callback from(Callback callback1, Callback callback2)
      Creates a callback which combines two other callbacks and will succeed or fail them both.
      Parameters:
      callback1 - The first callback
      callback2 - The second callback
      Returns:
      a new callback.
    • combine

      static Callback combine(Callback cb1, Callback cb2)