Interface ActionListener<Response>

All Known Subinterfaces:
PersistentTasksService.WaitForPersistentTaskListener<P>, RejectAwareActionListener<T>
All Known Implementing Classes:
ChannelActionListener, ContextPreservingActionListener, CountDownActionListener, DelegatingActionListener, FinalizeSnapshotContext, GetSnapshotInfoContext, GroupedActionListener, LatchedActionListener, ListenableActionFuture, ListenableFuture, LoggingTaskListener, PlainActionFuture, RestActionListener, RestActions.NodesResponseRestListener, RestBuilderListener, RestChunkedToXContentListener, RestResponseListener, RestToXContentListener, SearchExecutionStatsCollector, SearchProgressActionListener, SnapshotShardContext, SubscribableListener, ThreadedActionListener

public interface ActionListener<Response>
A listener for action responses or failures.
  • 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(Exception e)
      A failure caused by an exception at some phase of the task.
    • noop

      static <T> ActionListener<T> noop()
      Returns:
      a listener that does nothing
    • map

      default <T> ActionListener<T> map(CheckedFunction<T,Response,Exception> fn)
      Creates a listener that wraps this listener, mapping response values via the given mapping function and passing along exceptions to this instance. Notice that it is considered a bug if the listener's onResponse or onFailure fails. onResponse failures will not call onFailure. If the function fails, the listener's onFailure handler will be called. The principle is that the mapped listener will handle exceptions from the mapping function fn but it is the responsibility of delegate to handle its own exceptions inside `onResponse` and `onFailure`.
      Type Parameters:
      T - Response type of the wrapped listener
      Parameters:
      fn - Function to apply to listener response
      Returns:
      a listener that maps the received response and then passes it to this instance
    • safeMap

      default <T> ActionListener<T> safeMap(Function<T,Response> fn)
      Same as map(CheckedFunction) except that fn is expected to never throw.
    • delegateResponse

      Creates a listener that delegates all responses it receives to this instance.
      Parameters:
      bc - BiConsumer invoked with delegate listener and exception
      Returns:
      Delegating listener
    • delegateFailure

      default <T> ActionListener<T> delegateFailure(BiConsumer<ActionListener<Response>,T> bc)
      Creates a listener that delegates all exceptions it receives to another listener.
      Type Parameters:
      T - Type of the delegating listener's response
      Parameters:
      bc - BiConsumer invoked with delegate listener and response
      Returns:
      Delegating listener
    • delegateFailureAndWrap

      default <T> ActionListener<T> delegateFailureAndWrap(CheckedBiConsumer<ActionListener<Response>,T,? extends Exception> bc)
      Same as delegateFailure(BiConsumer) except that any failure thrown by bc or the delegate listener's onResponse(Response) will be passed to the delegate listeners onFailure(Exception).
    • releasing

      static <Response> ActionListener<Response> releasing(Releasable releasable)
      Creates a listener which releases the given resource on completion (whether success or failure)
    • running

      static <Response> ActionListener<Response> running(Runnable runnable)
      Creates a listener that executes the given runnable on completion (whether successful or otherwise).
      Type Parameters:
      Response - the type of the response, which is ignored.
      Parameters:
      runnable - the runnable that will be called in event of success or failure. This must not throw.
      Returns:
      a listener that executes the given runnable on completion (whether successful or otherwise).
    • wrap

      @Deprecated(forRemoval=true) static <Response> ActionListener<Response> wrap(Runnable runnable)
      Deprecated, for removal: This API element is subject to removal in a future version.
      in favour of running(Runnable) because this implementation doesn't "wrap" exceptions from onResponse(Response) into onFailure(java.lang.Exception).
    • wrap

      static <Response> ActionListener<Response> wrap(CheckedConsumer<Response,? extends Exception> onResponse, Consumer<Exception> onFailure)
      Creates a listener that executes the appropriate consumer when the response (or failure) is received. This listener is "wrapped" in the sense that an exception from the onResponse consumer is passed into the onFailure consumer.

      If the onFailure argument is listener::onFailure for some other ActionListener, prefer to use delegateFailureAndWrap(org.elasticsearch.common.CheckedBiConsumer<org.elasticsearch.action.ActionListener<Response>, T, ? extends java.lang.Exception>) instead.

      Type Parameters:
      Response - the type of the response
      Parameters:
      onResponse - the checked consumer of the response, executed when the listener is completed successfully. If it throws an exception, the exception is passed to the onFailure consumer.
      onFailure - the consumer of the failure, executed when the listener is completed with an exception (or it is completed successfully but the onResponse consumer threw an exception).
      Returns:
      a listener that executes the appropriate consumer when the response (or failure) is received.
    • wrap

      static <DelegateResponse, Response extends DelegateResponse> ActionListener<Response> wrap(ActionListener<DelegateResponse> delegate)
      Adds a wrapper around a listener which catches exceptions thrown by its onResponse(Response) method and feeds them to its onFailure(java.lang.Exception) method.
    • onResponse

      static <Response> void onResponse(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(Iterable<ActionListener<Response>> listeners, 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, 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.
    • releaseAfter

      static <Response> ActionListener<Response> releaseAfter(ActionListener<Response> delegate, Releasable releaseAfter)
      Wraps a given listener and returns a new listener which releases the provided releaseAfter resource 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 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. If the supplier fails, the listener's onFailure handler will be called. It is the responsibility of delegate to handle its own exceptions inside `onResponse` and `onFailure`.
    • assertOnce

      static <Response> ActionListener<Response> assertOnce(ActionListener<Response> delegate)
      Returns:
      A listener which (if assertions are enabled) wraps around the given delegate and asserts that it is only called once.
    • run

      static <T, L extends ActionListener<T>> void run(L listener, CheckedConsumer<L,Exception> action)
      Execute the given action in a try/catch block which feeds all exceptions to the given listener's onFailure(java.lang.Exception) method.