Class StepListener<Response>

All Implemented Interfaces:

public final class StepListener<Response> extends NotifyOnceListener<Response>
A StepListener provides a simple way to write a flow consisting of multiple asynchronous steps without having nested callbacks. For example:

  void asyncFlowMethod(... ActionListener<R> flowListener) {
    StepListener<R1> step1 = new StepListener<>();
    asyncStep1(..., step1);

    StepListener<R2> step2 = new StepListener<>();
    step1.whenComplete(r1 -> {
      asyncStep2(r1, ..., step2);
    }, flowListener::onFailure);

    step2.whenComplete(r2 -> {
      R1 r1 = step1.result();
      R r = combine(r1, r2);
    }, flowListener::onFailure);
  • Constructor Details

    • StepListener

      public StepListener()
  • Method Details

    • innerOnResponse

      protected void innerOnResponse(Response response)
      Specified by:
      innerOnResponse in class NotifyOnceListener<Response>
    • innerOnFailure

      protected void innerOnFailure(Exception e)
      Specified by:
      innerOnFailure in class NotifyOnceListener<Response>
    • whenComplete

      public void whenComplete(CheckedConsumer<Response,Exception> onResponse, Consumer<Exception> onFailure)
      Registers the given actions which are called when this step is completed. If this step is completed successfully, the onResponse is called with the result; otherwise the onFailure is called with the failure.
      onResponse - is called when this step is completed successfully
      onFailure - is called when this step is completed with a failure
    • thenCombine

      public <OtherResponse, OuterResponse> StepListener<OuterResponse> thenCombine(StepListener<OtherResponse> other, BiFunction<Response,OtherResponse,OuterResponse> fn)
      Combines this listener with another one, waiting for both to successfully complete and combining their results.
      other - the other step listener to combine with
      fn - the function that combines the results
      the combined listener
    • asFuture

      public Future<Response> asFuture()
      Returns the future associated with the given step listener
    • result

      public Response result()
      Gets the result of this step. This method will throw IllegalStateException if this step is not completed yet.
    • addListener

      public void addListener(ActionListener<Response> listener)
      Registers the given listener to be notified with the result of this step.