Class Promise.Task<T>

java.lang.Object
org.eclipse.jetty.util.Promise.Task<T>
Type Parameters:
T - the type of the result of the task
All Implemented Interfaces:
Runnable, Promise<T>
Enclosing interface:
Promise<C>

public abstract static class Promise.Task<T> extends Object implements Promise<T>, Runnable

A Promise that implements Runnable to perform a one-shot task that eventually completes this Promise.

Subclasses override Runnable.run() to implement the task.

Users of this class start the task execution via Runnable.run().

Typical usage:

// Specify what to do in case of success and failure.
Promise.Task<T> task = new Promise.Task<>(() -> onSuccess(), x -> onFailure(x))
{
    @Override
    public void run()
    {
        try
        {
            // Perform some task.
            T result = performTask();

            // Eventually succeed this Promise.
            succeeded(result);
        }
        catch (Throwable x)
        {
            // Fail this Promise.
            failed(x);
        }
    }
}

// Start the task.
task.run();
  • Constructor Details

  • Method Details

    • succeeded

      public void succeeded(T result)
      Description copied from interface: Promise

      Callback invoked when the operation completes.

      Specified by:
      succeeded in interface Promise<T>
      Parameters:
      result - the context
      See Also:
    • failed

      public void failed(Throwable x)
      Description copied from interface: Promise

      Callback invoked when the operation fails.

      Specified by:
      failed in interface Promise<T>
      Parameters:
      x - the reason for the operation failure