Interface ActionListener<Response>

All Known Subinterfaces:
PersistentTasksService.WaitForPersistentTaskListener<P>, RejectAwareActionListener<T>
All Known Implementing Classes:
AdapterActionFuture, ChannelActionListener, ContextPreservingActionListener, GroupedActionListener, LatchedActionListener, ListenableFuture, NotifyOnceListener, PlainActionFuture, PlainListenableActionFuture, RestActionListener, RestActions.NodesResponseRestListener, RestBuilderListener, RestResponseListener, RestStatusToXContentListener, RestToXContentListener, SearchExecutionStatsCollector, SearchProgressActionListener, StepListener, ThreadedActionListener

public interface ActionListener<Response>
A listener for action responses or failures.
  • Method Summary

    Modifier and Type Method Description
    static <Response> void completeWith​(ActionListener<Response> listener, CheckedSupplier<Response,​? extends java.lang.Exception> supplier)
    Completes the given listener with the result from the provided supplier accordingly.
    static <T,​ R> ActionListener<T> delegateFailure​(ActionListener<R> delegate, java.util.function.BiConsumer<ActionListener<R>,​T> bc)
    Creates a listener that delegates all exceptions it receives to another listener.
    static <T> ActionListener<T> delegateResponse​(ActionListener<T> delegate, java.util.function.BiConsumer<ActionListener<T>,​java.lang.Exception> bc)
    Creates a listener that delegates all responses it receives to another listener.
    static <T,​ Response>
    ActionListener<Response>
    map​(ActionListener<T> listener, CheckedFunction<Response,​T,​java.lang.Exception> fn)
    Creates a listener that wraps another listener, mapping response values via the given mapping function and passing along exceptions to the delegate.
    static <Response> ActionListener<Response> notifyOnce​(ActionListener<Response> delegate)
    Wraps a given listener and returns a new listener which makes sure onResponse(Object) and onFailure(Exception) of the provided listener will be called at most once.
    void onFailure​(java.lang.Exception e)
    A failure caused by an exception at some phase of the task.
    static <Response> void onFailure​(java.lang.Iterable<ActionListener<Response>> listeners, java.lang.Exception failure)
    Notifies every given listener with the failure passed to onFailure(Exception).
    static <Response> void onResponse​(java.lang.Iterable<ActionListener<Response>> listeners, Response response)
    Notifies every given listener with the response passed to onResponse(Object).
    void onResponse​(Response response)
    Handle action response.
    static <Response> ActionListener<Response> runAfter​(ActionListener<Response> delegate, java.lang.Runnable runAfter)
    Wraps a given listener and returns a new listener which executes the provided runAfter callback when the listener is notified via either #onResponse or #onFailure.
    static <Response> ActionListener<Response> runBefore​(ActionListener<Response> delegate, CheckedRunnable<?> runBefore)
    Wraps a given listener and returns a new listener which executes the provided runBefore callback before the listener is notified via either #onResponse or #onFailure.
    static <Response> java.util.function.BiConsumer<Response,​java.lang.Exception> toBiConsumer​(ActionListener<Response> listener)
    Converts a listener to a BiConsumer for compatibility with the CompletableFuture api.
    static <Response> ActionListener<Response> wrap​(java.lang.Runnable runnable)
    Creates a listener that listens for a response (or failure) and executes the corresponding runnable when the response (or failure) is received.
    static <Response> ActionListener<Response> wrap​(CheckedConsumer<Response,​? extends java.lang.Exception> onResponse, java.util.function.Consumer<java.lang.Exception> onFailure)
    Creates a listener that listens for a response (or failure) and executes the corresponding consumer when the response (or failure) is received.
  • Method Details

    • onResponse

      void onResponse​(Response response)
      Handle action response. This response may constitute a failure or a success but it is up to the listener to make that decision.
    • onFailure

      void onFailure​(java.lang.Exception e)
      A failure caused by an exception at some phase of the task.
    • wrap

      static <Response> ActionListener<Response> wrap​(CheckedConsumer<Response,​? extends java.lang.Exception> onResponse, java.util.function.Consumer<java.lang.Exception> onFailure)
      Creates a listener that listens for a response (or failure) and executes the corresponding consumer when the response (or failure) is received.
      Type Parameters:
      Response - the type of the response
      Parameters:
      onResponse - the checked consumer of the response, when the listener receives one
      onFailure - the consumer of the failure, when the listener receives one
      Returns:
      a listener that listens for responses and invokes the consumer when received
    • delegateResponse

      static <T> ActionListener<T> delegateResponse​(ActionListener<T> delegate, java.util.function.BiConsumer<ActionListener<T>,​java.lang.Exception> bc)
      Creates a listener that delegates all responses it receives to another listener.
      Type Parameters:
      T - Type of the listener
      Parameters:
      delegate - ActionListener to wrap and delegate any exception to
      bc - BiConsumer invoked with delegate listener and exception
      Returns:
      Delegating listener
    • delegateFailure

      static <T,​ R> ActionListener<T> delegateFailure​(ActionListener<R> delegate, java.util.function.BiConsumer<ActionListener<R>,​T> bc)
      Creates a listener that delegates all exceptions it receives to another listener.
      Type Parameters:
      T - Type of the delegating listener's response
      R - Type of the wrapped listeners
      Parameters:
      delegate - ActionListener to wrap and delegate any exception to
      bc - BiConsumer invoked with delegate listener and response
      Returns:
      Delegating listener
    • wrap

      static <Response> ActionListener<Response> wrap​(java.lang.Runnable runnable)
      Creates a listener that listens for a response (or failure) and executes the corresponding runnable when the response (or failure) is received.
      Type Parameters:
      Response - the type of the response
      Parameters:
      runnable - the runnable that will be called in event of success or failure
      Returns:
      a listener that listens for responses and invokes the runnable when received
    • map

      static <T,​ Response> ActionListener<Response> map​(ActionListener<T> listener, CheckedFunction<Response,​T,​java.lang.Exception> fn)
      Creates a listener that wraps another listener, mapping response values via the given mapping function and passing along exceptions to the delegate.
      Type Parameters:
      Response - Response type of the new listener
      T - Response type of the wrapped listener
      Parameters:
      listener - Listener to delegate to
      fn - Function to apply to listener response
      Returns:
      a listener that maps the received response and then passes it to its delegate listener
    • toBiConsumer

      static <Response> java.util.function.BiConsumer<Response,​java.lang.Exception> toBiConsumer​(ActionListener<Response> listener)
      Converts a listener to a BiConsumer for compatibility with the CompletableFuture api.
      Type Parameters:
      Response - the type of the response
      Parameters:
      listener - that will be wrapped
      Returns:
      a bi consumer that will complete the wrapped listener
    • onResponse

      static <Response> void onResponse​(java.lang.Iterable<ActionListener<Response>> listeners, Response response)
      Notifies every given listener with the response passed to onResponse(Object). If a listener itself throws an exception the exception is forwarded to onFailure(Exception). If in turn onFailure(Exception) fails all remaining listeners will be processed and the caught exception will be re-thrown.
    • onFailure

      static <Response> void onFailure​(java.lang.Iterable<ActionListener<Response>> listeners, java.lang.Exception failure)
      Notifies every given listener with the failure passed to onFailure(Exception). If a listener itself throws an exception all remaining listeners will be processed and the caught exception will be re-thrown.
    • runAfter

      static <Response> ActionListener<Response> runAfter​(ActionListener<Response> delegate, java.lang.Runnable runAfter)
      Wraps a given listener and returns a new listener which executes the provided runAfter callback when the listener is notified via either #onResponse or #onFailure.
    • runBefore

      static <Response> ActionListener<Response> runBefore​(ActionListener<Response> delegate, CheckedRunnable<?> runBefore)
      Wraps a given listener and returns a new listener which executes the provided runBefore callback before the listener is notified via either #onResponse or #onFailure. If the callback throws an exception then it will be passed to the listener's #onFailure and its #onResponse will not be executed.
    • notifyOnce

      static <Response> ActionListener<Response> notifyOnce​(ActionListener<Response> delegate)
      Wraps a given listener and returns a new listener which makes sure onResponse(Object) and onFailure(Exception) of the provided listener will be called at most once.
    • completeWith

      static <Response> void completeWith​(ActionListener<Response> listener, CheckedSupplier<Response,​? extends java.lang.Exception> supplier)
      Completes the given listener with the result from the provided supplier accordingly. This method is mainly used to complete a listener with a block of synchronous code.