Module org.elasticsearch.xcore
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 Summary
ConstructorsConstructorDescriptionIteratingActionListener(ActionListener<T> delegate, BiConsumer<U, ActionListener<T>> consumer, List<U> consumables, ThreadContext threadContext) Constructs anIteratingActionListener.IteratingActionListener(ActionListener<T> delegate, BiConsumer<U, ActionListener<T>> consumer, List<U> consumables, ThreadContext threadContext, Function<T, T> finalResultFunction) Constructs anIteratingActionListener.IteratingActionListener(ActionListener<T> delegate, BiConsumer<U, ActionListener<T>> consumer, List<U> consumables, ThreadContext threadContext, Function<T, T> finalResultFunction, Predicate<T> iterationPredicate) Constructs anIteratingActionListener. -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.elasticsearch.action.ActionListener
delegateFailure, delegateFailureAndWrap, delegateFailureIgnoreResponseAndWrap, delegateResponse, map, safeMap
-
Constructor Details
-
IteratingActionListener
public IteratingActionListener(ActionListener<T> delegate, BiConsumer<U, ActionListener<T>> consumer, List<U> consumables, ThreadContext threadContext) Constructs anIteratingActionListener.- Parameters:
delegate- the delegate listener to call when all consumables have finished executingconsumer- the consumer that is executed for each consumable instanceconsumables- the instances that can be consumed to produce a response which is ultimately sent on the delegate listenerthreadContext- 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) Constructs anIteratingActionListener.- Parameters:
delegate- the delegate listener to call when all consumables have finished executingconsumer- the consumer that is executed for each consumable instanceconsumables- the instances that can be consumed to produce a response which is ultimately sent on the delegate listenerthreadContext- the thread context for the thread pool that created the listenerfinalResultFunction- 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) Constructs anIteratingActionListener.- Parameters:
delegate- the delegate listener to call when all consumables have finished executingconsumer- the consumer that is executed for each consumable instanceconsumables- the instances that can be consumed to produce a response which is ultimately sent on the delegate listenerthreadContext- the thread context for the thread pool that created the listenerfinalResultFunction- 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- aPredicatethat checks if iteration should continue based on the returned result
-
-
Method Details
-
run
public void run() -
onResponse
- Specified by:
onResponsein interfaceActionListener<T>
-
onFailure
- Specified by:
onFailurein interfaceActionListener<T>
-