Class IteratingActionListener<T,U>

java.lang.Object
org.elasticsearch.xpack.core.common.IteratingActionListener<T,U>
All Implemented Interfaces:
Runnable, ActionListener<T>

public final class IteratingActionListener<T,U> extends Object implements ActionListener<T>, Runnable
This action listener wraps another listener and provides a framework for iteration over a List while calling an asynchronous function for each. The listener calls the BiConsumer with the current element in the list and a ActionListener. This function is expected to call the listener in case of success or failure due to an exception. If there is a failure due to an exception the wrapped listener's ActionListener.onFailure(Exception) method is called. If the consumer calls onResponse(Object) with a non-null object, iteration will cease and the wrapped listener will be called with the response. In the case of a null value being passed to onResponse(Object) then iteration will continue by applying the BiConsumer to the next item in the list; if the list has no more elements then the wrapped listener will be called with a null value, unless an optional Supplier is provided that supplies the response to send for ActionListener.onResponse(Object). After creation, iteration is started by calling run()
  • Constructor Details

    • IteratingActionListener

      public IteratingActionListener(ActionListener<T> delegate, BiConsumer<U,ActionListener<T>> consumer, List<U> consumables, ThreadContext threadContext)
      Parameters:
      delegate - the delegate listener to call when all consumables have finished executing
      consumer - the consumer that is executed for each consumable instance
      consumables - the instances that can be consumed to produce a response which is ultimately sent on the delegate listener
      threadContext - the thread context for the thread pool that created the listener
    • IteratingActionListener

      public IteratingActionListener(ActionListener<T> delegate, BiConsumer<U,ActionListener<T>> consumer, List<U> consumables, ThreadContext threadContext, Function<T,T> finalResultFunction)
      Parameters:
      delegate - the delegate listener to call when all consumables have finished executing
      consumer - the consumer that is executed for each consumable instance
      consumables - the instances that can be consumed to produce a response which is ultimately sent on the delegate listener
      threadContext - the thread context for the thread pool that created the listener
      finalResultFunction - a function that maps the response which terminated iteration to a response that will be sent to the delegate listener. This is useful if the delegate listener should receive some other value (perhaps a concatenation of the results of all the called consumables).
    • IteratingActionListener

      public IteratingActionListener(ActionListener<T> delegate, BiConsumer<U,ActionListener<T>> consumer, List<U> consumables, ThreadContext threadContext, Function<T,T> finalResultFunction, Predicate<T> iterationPredicate)
      Parameters:
      delegate - the delegate listener to call when all consumables have finished executing
      consumer - the consumer that is executed for each consumable instance
      consumables - the instances that can be consumed to produce a response which is ultimately sent on the delegate listener
      threadContext - the thread context for the thread pool that created the listener
      finalResultFunction - a function that maps the response which terminated iteration to a response that will be sent to the delegate listener. This is useful if the delegate listener should receive some other value (perhaps a concatenation of the results of all the called consumables).
      iterationPredicate - a Predicate that checks if iteration should continue based on the returned result
  • Method Details