Class CancellableFanOut<Item,ItemResponse,FinalResponse>
Note that it's easy to accidentally capture another reference to this class when implementing it, and this will prevent the early release
of any accumulated results. Beware of lambdas and method references. You must test your implementation carefully (using e.g.
ReachabilityChecker
) to make sure it doesn't do this.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract FinalResponse
Called when responses for all items have been processed, on the thread that processed the last per-item response or possibly the thread which calledrun(org.elasticsearch.tasks.Task, java.util.Iterator<Item>, org.elasticsearch.action.ActionListener<FinalResponse>)
if all items were processed beforerun(org.elasticsearch.tasks.Task, java.util.Iterator<Item>, org.elasticsearch.action.ActionListener<FinalResponse>)
returns.protected abstract void
onItemFailure
(Item item, Exception e) Handle a failure for an item.protected abstract void
onItemResponse
(Item item, ItemResponse itemResponse) Handle a successful response for an item.final void
run
(Task task, Iterator<Item> itemsIterator, ActionListener<FinalResponse> listener) Run the fan-out action.protected abstract void
sendItemRequest
(Item item, ActionListener<ItemResponse> listener) Run the action (typically by sending a transport request) for an individual item.
-
Constructor Details
-
CancellableFanOut
public CancellableFanOut()
-
-
Method Details
-
run
public final void run(@Nullable Task task, Iterator<Item> itemsIterator, ActionListener<FinalResponse> listener) Run the fan-out action.- Parameters:
task
- The task to watch for cancellations. Ifnull
or not aCancellableTask
then the fan-out still works, just without any cancellation handling.itemsIterator
- The items over which to fan out. Iterated on the calling thread.listener
- A listener for the final response, which is completed after all the fanned-out actions have completed. It is not completed promptly on cancellation. Completed on the thread that handles the final per-item response (or the calling thread if there are no items).
-
sendItemRequest
Run the action (typically by sending a transport request) for an individual item. Called in sequence on the thread that invokedrun(org.elasticsearch.tasks.Task, java.util.Iterator<Item>, org.elasticsearch.action.ActionListener<FinalResponse>)
. May not be called for every item if the task is cancelled during the iteration.Note that it's easy to accidentally capture another reference to this class when implementing this method, and that will prevent the early release of any accumulated results. Beware of lambdas, and test carefully.
-
onItemResponse
Handle a successful response for an item. May be called concurrently for multiple items. Not called if the task is cancelled. Must not throw any exceptions.Note that it's easy to accidentally capture another reference to this class when implementing this method, and that will prevent the early release of any accumulated results. Beware of lambdas, and test carefully.
-
onItemFailure
Handle a failure for an item. May be called concurrently for multiple items. Not called if the task is cancelled. Must not throw any exceptions.Note that it's easy to accidentally capture another reference to this class when implementing this method, and that will prevent the early release of any accumulated results. Beware of lambdas, and test carefully.
-
onCompletion
Called when responses for all items have been processed, on the thread that processed the last per-item response or possibly the thread which calledrun(org.elasticsearch.tasks.Task, java.util.Iterator<Item>, org.elasticsearch.action.ActionListener<FinalResponse>)
if all items were processed beforerun(org.elasticsearch.tasks.Task, java.util.Iterator<Item>, org.elasticsearch.action.ActionListener<FinalResponse>)
returns. Not called if the task is cancelled.Note that it's easy to accidentally capture another reference to this class when implementing this method, and that will prevent the early release of any accumulated results. Beware of lambdas, and test carefully.
- Throws:
Exception
-